标签:
腾讯的IP地址API接口地址:http://fw.qq.com/ipaddress
返回的是数据格式为:
1 var IPData = new Array(“58.218.198.205″,”",”江苏省”,”徐州市”);
使用JS代码进行调取:
1 2
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=12.130.132.30
搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip
#pragma mark 获取设备IP//(局域网内部IP外部的2G/3G/4G网络获取不到) // Get IP Address +(NSString *)getIPAddress { 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; } //*************************************
//通过搜狐的IP地址查询接口进行编码获取;
//可以获取非WiFi网络,但是WiFi网络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; }
标签:
原文地址:http://www.cnblogs.com/OIMM/p/5620058.html