基于ip地址对当前的请求位置的判断

-(NSString *)getWANIP
{
    //通过淘宝的服务来定位WAN的IP,否则获取路由IP没什么用
    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*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"str--->%@",str);
    
    NSString *ipStr = nil;
    if (ipDic && [ipDic[@"code"] integerValue] == 0) { //获取成功
        ipStr = ipDic[@"data"][@"country"];
    }
    
    self.dataDic=ipDic[@"data"];
    
    self.dataArr=[ipDic[@"data"] allKeys];
    
    [_myTableView reloadData];
    
    
    return (ipStr ? ipStr : @"");
}

你可能感兴趣的:(基于ip地址对当前的请求位置的判断)