美文网首页
获取设备的ip地址

获取设备的ip地址

作者: 杨闯 | 来源:发表于2018-10-09 21:27 被阅读27次

Mac原生

#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>

@interface IpModel:NSObject
@property (nonatomic, copy) NSString *ipAddress;
@property (nonatomic, copy) NSString *ifaName;
@end

@implementation IpModel
@end

NSArray *getDeviceIPAdress() {
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = getifaddrs(&interfaces);
    
    NSMutableArray *ipArray = [[NSMutableArray alloc] init];
    
    if (success == 0) { // 0 表示获取成功
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            if( temp_addr->ifa_addr->sa_family == AF_INET)
            {
                IpModel *ip = [[IpModel alloc] init];
                ip.ipAddress = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                ip.ifaName = [NSString stringWithUTF8String:temp_addr->ifa_name];
                [ipArray addObject:ip];
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    freeifaddrs(interfaces);
    return ipArray;
}

Electron

先安装依赖 npm install ip
代码里面

const ip = require('ip');
const IPAddress = ip.address();
console.log(IPAddress);

c#

相关文章

网友评论

      本文标题:获取设备的ip地址

      本文链接:https://www.haomeiwen.com/subject/pfuyaftx.html