专业知识分享(iOS)iOS知识收藏

(已过时,待更新内容,谢谢)iOS 获取IP地址

2019-03-28  本文已影响0人  LouisDrink

因为项目中加入的第三方数据统计SDK中需要上传IP地址,之前没有接触过IP地址上传,于是搜索了iOS中获取设备ip地址的相关资料并加以整理形成本文。

获取IP地址主要有以下两种情况:

WIFI情况下获取内网IP地址:
// 需导入以下头文件
#import <ifaddrs.h>
#import <arpa/inet.h>

// Get IP Address
- (NSString *)GetOurIpAddress {

    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            if(temp_addr->ifa_addr->sa_family == AF_INET) {
                // Check if interface is en0 which is the wifi connection on the iPhone
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return address;
}
WIFI情况下获取外网IP地址 与 数据流量情况下获取手机IP地址

上述两种情况(WIFI下外网IP与数据下手机IP)皆可用下述代码获取IP

通过访问搜狐的citySN获取IP信息
-(NSDictionary *)deviceWANIPAdress{
    
    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解析
        //删除字符串多余字符串
        NSRange range = NSMakeRange(0, 19);
        [ip deleteCharactersInRange: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;
    }
    return nil;
}

通过上面方法会返回一个包含IP信息的字典,信息内容如下:

{
    // 邮政编码
    cid = 350200;
    // IP
    cip = "211.97.130.169";
    // 地理信息
    cname = "\U798f\U5efa\U7701\U53a6\U95e8\U5e02";
}

Tips:

通过淘宝的getIpInfo服务获取IP
-(NSDictionary *)deviceWANIPAdress{
    
    NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo2.php?ip=myip"];
    
    NSData *data = [NSData dataWithContentsOfURL:ipURL];
    NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    return ipDic;
}

通过上面方法会返回一个包含IP信息的字典,信息内容如下:

    {
        area = "";
        "area_id" = "";
        city = "\U53a6\U95e8";
        "city_id" = 350200;
        country = "\U4e2d\U56fd";
        "country_id" = CN;
        county = XX;
        "county_id" = xx;
        ip = "183.250.89.75";
        isp = "\U79fb\U52a8";
        "isp_id" = 100025;
        region = "\U798f\U5efa";
        "region_id" = 350000;
    }

相比之下,淘宝的IP信息返回多了些信息,如运营商等。其余并无太大差别,根据自己的需求任意选择即可
Tips:网络请求记得使用异步方法

上一篇 下一篇

猜你喜欢

热点阅读