iOS 获取ip地址

 1 #pragma mark - 获取设备当前网络IP地址
 2 + (NSString *)getIPAddress:(BOOL)preferIPv4
 3 {
 4     NSArray *searchArray = preferIPv4 ?
 5     @[ 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 ] :
 6     @[ 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 ] ;
 7     
 8     NSDictionary *addresses = [self getIPAddresses];
 9     NSLog(@"addresses: %@", addresses);
10     
11     __block NSString *address;
12     [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
13      {
14          address = addresses[key];
15          //筛选出IP地址格式
16          if([self isValidatIP:address]) *stop = YES;
17      } ];
18     return address ? address : @"0.0.0.0";
19 }
20 
21 + (BOOL)isValidatIP:(NSString *)ipAddress {
22     if (ipAddress.length == 0) {
23         return NO;
24     }
25     NSString *urlRegEx = @"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
26     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
27     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
28     "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
29     
30     NSError *error;
31     NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlRegEx options:0 error:&error];
32     
33     if (regex != nil) {
34         NSTextCheckingResult *firstMatch=[regex firstMatchInString:ipAddress options:0 range:NSMakeRange(0, [ipAddress length])];
35         
36         if (firstMatch) {
37             NSRange resultRange = [firstMatch rangeAtIndex:0];
38             NSString *result=[ipAddress substringWithRange:resultRange];
39             //输出结果
40             NSLog(@"%@",result);
41             return YES;
42         }
43     }
44     return NO;
45 }
46 
47 + (NSDictionary *)getIPAddresses
48 {
49     NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
50     
51     // retrieve the current interfaces - returns 0 on success
52     struct ifaddrs *interfaces;
53     if(!getifaddrs(&interfaces)) {
54         // Loop through linked list of interfaces
55         struct ifaddrs *interface;
56         for(interface=interfaces; interface; interface=interface->ifa_next) {
57             if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
58                 continue; // deeply nested code harder to read
59             }
60             const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
61             char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
62             if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
63                 NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
64                 NSString *type;
65                 if(addr->sin_family == AF_INET) {
66                     if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
67                         type = IP_ADDR_IPv4;
68                     }
69                 } else {
70                     const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
71                     if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
72                         type = IP_ADDR_IPv6;
73                     }
74                 }
75                 if(type) {
76                     NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
77                     addresses[key] = [NSString stringWithUTF8String:addrBuf];
78                 }
79             }
80         }
81         // Free memory
82         freeifaddrs(interfaces);
83     }
84     return [addresses count] ? addresses : nil;
85 }

 

posted @ 2017-02-28 16:03  福泽小院  阅读(1110)  评论(0编辑  收藏  举报