iOS手机内网和外网IP地址的获取

2019-05-21  本文已影响0人  异乡人_4f2a

需要导入的头文件

#import <ifaddrs.h> 

#import <arpa/inet.h>

#define IOS_CELLULAR    @"pdp_ip0" 

#define IOS_WIFI        @"en0" 

#define IOS_VPN         @"utun0"

#define IP_ADDR_IPv4    @"ipv4"

#define IP_ADDR_IPv6    @"ipv6"


#pragma mark -- 获取外网IP地址

+ (NSString*)getWANAddresses {

    NSError*error;

    NSURL *ipURL = [NSURL URLWithString:@"http://pv.sohu.com/cityjson?ie=utf-8"];

    NSMutableString *ip = [NSMutableString stringWithContentsOfURL:ipURL encoding:NSUTF8StringEncoding error:&error];

    //判断返回字符串是否为所需数据

    if ([ip hasPrefix:@"var returnCitySN = "]) {

        //对字符串进行处理,然后进行json解析

        //删除字符串多余字符串

        NSRangerange =NSMakeRange(0,19);

        [ipdeleteCharactersInRange:range];

        NSString * nowIp =[ip substringToIndex:ip.length-1];

        //将字符串转换成二进制进行Json解析

        NSData * data = [nowIp dataUsingEncoding:NSUTF8StringEncoding];

        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        return dict[@"cip"] ? dict[@"cip"] :@"";

    }

    return @"";

}

#pragma mark -- 获取内网IP地址

+ (NSString*)getIPAddresses {

    NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];

    // retrieve the current interfaces - returns 0 on success

    structifaddrs*interfaces;

    if(!getifaddrs(&interfaces)) {

        // Loop through linked list of interfaces

        structifaddrs*interface;

        for(interface=interfaces; interface; interface=interface->ifa_next) {

            if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {

                continue; // deeply nested code harder to read

            }

            conststructsockaddr_in*addr = (conststructsockaddr_in*)interface->ifa_addr;

            char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];

            if(addr && (addr->sin_family==AF_INET|| addr->sin_family==AF_INET6)) {

                NSString *name = [NSString stringWithUTF8String:interface->ifa_name];

                NSString*type;

                if(addr->sin_family==AF_INET) {

                    if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf,INET_ADDRSTRLEN)) {

                        type =IP_ADDR_IPv4;

                    }

                }else{

                    conststructsockaddr_in6*addr6 = (conststructsockaddr_in6*)interface->ifa_addr;

                    if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf,INET6_ADDRSTRLEN)) {

                        type =IP_ADDR_IPv6;

                    }

                }

                if(type) {

                    NSString*key = [NSStringstringWithFormat:@"%@/%@", name, type];

                    addresses[key] = [NSStringstringWithUTF8String:addrBuf];

                }

            }

        }

        // Free memory

        freeifaddrs(interfaces);

    }

    if([addressescount]) {

        //en0/ipv4:WIFI

        //pdp_ip0/ipv4:WWAN(4G)

        returnhasLen(addresses[@"en0/ipv4"]) ? addresses[@"en0/ipv4"] : addresses[@"pdp_ip0/ipv4"];

    }else{

        returnnil;

    }

}

上一篇下一篇

猜你喜欢

热点阅读