好的,你是如何用IP做到的.
因此,geoplugin.net拥有令人惊叹的JSON api,它将IP默认为当前连接,所以您需要做的是向此地址发出请求:
http://www.geoplugin.net/json.gp
Astunishing!对我来说,它返回这些数据:
geoPlugin({
  "geoplugin_request":"201.6.226.233",
  "geoplugin_status":200,
  "geoplugin_city":null,
  "geoplugin_region":"São Paulo",
  "geoplugin_areaCode":0,
  "geoplugin_dmaCode":0,
  "geoplugin_countryCode":"BR",
  "geoplugin_countryName":"Brazil",
  "geoplugin_continentCode":"SA",
  "geoplugin_latitude":-23.473301,
  "geoplugin_longitude":-46.665798,
  "geoplugin_regionCode":27,
  "geoplugin_regionName":"São Paulo",
  "geoplugin_currencyCode":"BRL",
  "geoplugin_currencySymbol":"R$",
  "geoplugin_currencyConverter":2.0198
})
那么,现在你要做的就是解析这个"JSON".它实际上不是JSON,因为它有这个geoplugin( {data} )包装器.所以你可以懒惰地执行一些过滤,删除NSSTring的那些部分,也许.
我看你很匆忙,所以我利用业余时间为你写了一些代码.这是非标准的,因为我不知道你是否使用任何有用的REST框架,但是这里有:
NSString *url = [NSString stringWithFormat:@"http://www.geoplugin.net/json.gp"];
    NSString *locationData = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]
                                                encoding:NSUTF8StringEncoding]];
    locationData = [locationData stringByReplacingOccurrencesOfString:@"geoPlugin(" withString:@""];
    locationData = [locationData stringByReplacingOccurrencesOfString:@")" withString:@""];
    //user some json parser here.
    if([[[locationData JSONValue] valueForkey:@"geoplugin_countryCode"] isEqualToString:@"US"]){
        //proceed.
    }
我首先尝试检查运营商的移动国家代码.它将告诉您用户的蜂窝服务提供商的数字移动国家/地区代码,除非用户切换到其他国家/地区的服务提供商,否则用户无法更改.
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netInfo subscriberCellularProvider];
NSString *mcc = [carrier mobileCountryCode];
这当然不适用于未连接到移动运营商(iPod Touch,某些iPad等)的设备.因此,作为后退,我将使用或创建自己的IP地理定位 API.您可以在国家/地区级别获得99.5%准确率的免费数据库.这将非常适合检测没有移动提供商的设备的国家/地区.
| 归档时间: | 
 | 
| 查看次数: | 3666 次 | 
| 最近记录: |