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);
网友评论