(已過時,待更新內(nèi)容,謝謝)iOS 獲取IP地址

因為項目中加入的第三方數(shù)據(jù)統(tǒng)計SDK中需要上傳IP地址,之前沒有接觸過IP地址上傳,于是搜索了iOS中獲取設(shè)備ip地址的相關(guān)資料并加以整理形成本文。

獲取IP地址主要有以下兩種情況:

WIFI情況下獲取內(nèi)網(wǎng)IP地址:
// 需導(dǎo)入以下頭文件
#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情況下獲取外網(wǎng)IP地址 與 數(shù)據(jù)流量情況下獲取手機IP地址

上述兩種情況(WIFI下外網(wǎng)IP與數(shù)據(jù)下手機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];
    //判斷返回字符串是否為所需數(shù)據(jù)
    if ([ip hasPrefix:@"var returnCitySN = "]) {
        //對字符串進行處理,然后進行json解析
        //刪除字符串多余字符串
        NSRange range = NSMakeRange(0, 19);
        [ip deleteCharactersInRange:range];
        NSString * nowIp =[ip substringToIndex:ip.length-1];
        //將字符串轉(zhuǎn)換成二進制進行Json解析
        NSData * data = [nowIp dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        return dict;
    }
    return nil;
}

通過上面方法會返回一個包含IP信息的字典,信息內(nèi)容如下:

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

Tips:

  • WIFI情況下獲取到的IP地址中的cname,一般情況下只有精確到省份,而數(shù)據(jù)流量情況下能精確到市
  • 上述兩種情況下獲取到的郵政編碼一致。
  • cname中的內(nèi)容以Unicode編碼的形式返回,如需使用還需處理
通過淘寶的getIpInfo服務(wù)獲取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信息的字典,信息內(nèi)容如下:

    {
        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信息返回多了些信息,如運營商等。其余并無太大差別,根據(jù)自己的需求任意選擇即可
Tips:網(wǎng)絡(luò)請求記得使用異步方法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容