1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #import <ifaddrs.h> #import <arpa/inet.h> #include <netdb.h> #import <net/if.h>
#define IOS_CELLULAR @"pdp_ip0" // 蜂窝数据 #define IOS_WIFI @"en0" // WIFI #define IOS_VPN @"utun0" // VPN #define IP_ADDR_IPv4 @"ipv4" // IPv4 #define IP_ADDR_IPv6 @"ipv6" // IPv6
// 获取Ip地址(字符串) + (NSString *)getIpAddressWithIsIPv4:(BOOL)IPv4{ NSArray * IPv4Arr = @[ IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ]; NSArray * IPv6Arr = @[ IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ; NSArray * searchArray = IPv4 ? IPv4Arr : IPv6Arr; NSDictionary * addresses = [self getIpAddress]; NSLog(@"addresses: %@", addresses); __block NSString * address; [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop){ address = addresses[key]; if(address) * stop = YES; }]; return address ? address : @"0.0.0.0"; } // 获取所有相关IP信息(字典) + (NSDictionary *)getIpAddress{ NSMutableDictionary * addresses = [NSMutableDictionary dictionaryWithCapacity:8]; /** lo 本地回环 (Loopback) en 无线网 (WiFi) pdp_ip 蜂窝网络 (Cellular) awdl 通信Wi-Fi (Apple Wireless Direct Link,用于AirDrop、AirPlay、GameKit 和 Apple Pencil ) utun 虚拟隧道 (VPN) ipsec 虚拟隧道 (VPN) llw 低延迟无线局域网(Low-latency WLAN) */ // 定义 ifaddrs 类型的 指针变量,初始化为 NULL struct ifaddrs * interfaces = NULL; /* getifaddrs:用于获取系统上网络接口信息的函数 包含头文件<ifaddrs.h> struct ifaddrs ** : ifaddrs 类型的指针 返回 0 成功,非 0 出错 */ int success = getifaddrs(&interfaces); if(success) { struct ifaddrs *interface; for(interface = interfaces; interface; interface=interface->ifa_next) { if(!(interface->ifa_flags & IFF_UP)) { continue; } // 网络ip地址 const struct sockaddr_in * addr = (const struct sockaddr_in*)interface->ifa_addr; // // 子网掩码 // struct sockaddr_in * netmask_addr = (const struct sockaddr_in *)interface->ifa_netmask; char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ]; // IPv4 / IPv6 if(addr && (addr->sin_family == AF_INET || addr->sin_family == AF_INET6)) { NSString * name = [NSString stringWithUTF8String:interface->ifa_name]; NSString * type; NSString * netmask; // 网络地址族 if(addr->sin_family == AF_INET) { if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)){ type = IP_ADDR_IPv4; } // if (inet_ntoa(netmask_addr->sin_addr)) { // netmask = [NSString stringWithUTF8String:inet_ntoa(netmask_addr->sin_addr)];; // } } else { const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr; if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) { type = IP_ADDR_IPv6; } } if(type) { NSString *key = [NSString stringWithFormat:@"%@/%@", name, type]; addresses[key] = [NSString stringWithUTF8String:addrBuf]; } } } // Free memory freeifaddrs(interfaces); } return [addresses count] ? addresses : nil; }
|