- (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 : @"");
}此文转载,感谢原创作者的贡献。
本文介绍了一种通过调用淘宝提供的服务来获取设备外网IP地址的方法。利用NSURL和NSJSONSerialization等API实现网络请求及解析返回的JSON数据,从而得到IP地址所在国家的ID。
3638

被折叠的 条评论
为什么被折叠?



