iOS 根据IP获取位置

2018-03-06  本文已影响68人  KyleLou

- (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 : @"");

}

上一篇 下一篇

猜你喜欢

热点阅读