iOS 根据IP获取位置

- (NSString *)getWANIP {
    
    //通过淘宝提供的服务,定位WAN的IP
    NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];
    NSData *data = [NSData dataWithContentsOfURL:ipURL];
    NSDictionary *ipDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSString *ipStr = nil;
    NSLog(@"IPDict:%@",ipDict);
    //获取成功
    if (ipDict && [ipDict[@"code"] integerValue] == 0) {
        ipStr = ipDict[@"data"][@"country_id"];
    }
    return (ipStr ? ipStr : @"");
}


此文转载,感谢原创作者的贡献。

猜你喜欢

转载自blog.csdn.net/jacob_ios/article/details/79065500