iOS 获取外网IP

首推方法1:淘宝的接口http://ip.taobao.com/service/getIpInfo.php, 获取的到IP与百度IP是一样的

参数:
ip : IP地址 必填
如要本机的IP,填写myip

- (NSString *)deviceWANIPAddress  {  
    NSURL *ipURL = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=myip"];  
    NSData *data = [NSData dataWithContentsOfURL:ipURL];  
    NSDictionary *ipDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];  
    NSString *ipStr = nil;  
    if (ipDic && [ipDic[@"code"] integerValue] == 0) { //获取成功  
        ipStr = ipDic[@"data"][@"ip"];  
    }  
    return (ipStr ? ipStr : @"");  
} 

返回的数据为

{  
   code: 0,
   data: {  
            country: "中国",
            country_id: "CN",
            area: "华北",
            area_id: "100000",
            region: "北京市",
            region_id: "110000",
            city: "北京市",
            city_id: "110100",
            county: "",
            county_id: "-1",
            isp: "鹏博士",
            isp_id: "1000143",
            ip: "121.69.80.250"
          }
}
方法2: 新浪的接口 http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=115.29.28.108, 使用方式和淘宝的方式一样

参数:
format: 输出格式 json 可选 默认为JS
ip : IP地址 可选 默认为请求的IP
返回数据

{
  ret: 1,
  start: -1,
  end: -1,
  country: "中国",
  province: "北京",
  city: "北京",
  district: "",
  isp: "",
  type: "",
  desc: ""
}

还有一些没有测试

http://1111.ip138.com/ic.asp
https://ipip.yy.com/get_ip_info.php
http://pv.sohu.com/city[JS](http://bbs.125.la/thread-14068069-1-1.html)on?ie=utf-8
http://www.ip168.com/json.do?view=myipaddress
http://www.ipip.net/ip.php
http://ip.chemdrug.com/
http://ip.siteloop.net/
http://whois.pconline.com.cn/ipJson.jsp?callback={IP:255.255.255.255}
http://ip.siteloop.net/ajax/ip.php?ip=255.255.255.255&from=1
http://ip.siteloop.net/ajax/ip.php?ip=255.255.255.255&from=2
http://ip.siteloop.net/ajax/ip.php?ip=255.255.255.255&from=3
http://www.whatismyip.com.tw/

你可能感兴趣的:(iOS 获取外网IP)