iOS - 是美国的用户?

Alb*_*haw 7 location locale wifi ios

有没有办法让我检测用户是否在美国而不询问他们的位置?例如,当我在我的计算机上访问Fandango时,它知道我所在的县和州只是基于我的WiFi.

如果我有一台设备,也许是没有GPS的iPod Touch,我想验证它是在美国,并且它们连接到WiFi ...或者如果我有一台iPhone并且它已连接到3G和我想确保用户在美国,有没有一种方法可以检查信息而没有一点警报视图" __想要使用你当前的位置"?我不需要精确的坐标,我只需要确认这些用户是在美国(包括夏威夷和阿拉斯加州)......当地?

jtu*_*lla 7

好的,你是如何用IP做到的.

因此,geoplugin.net拥有令人惊叹的JSON api,它将IP默认为当前连接,所以您需要做的是向此地址发出请求:

http://www.geoplugin.net/json.gp
Run Code Online (Sandbox Code Playgroud)

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
})
Run Code Online (Sandbox Code Playgroud)

那么,现在你要做的就是解析这个"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.
    }
Run Code Online (Sandbox Code Playgroud)


Sco*_*ell 5

我首先尝试检查运营商的移动国家代码.它将告诉您用户的蜂窝服务提供商的数字移动国家/地区代码,除非用户切换到其他国家/地区的服务提供商,否则用户无法更改.

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netInfo subscriberCellularProvider];
NSString *mcc = [carrier mobileCountryCode];
Run Code Online (Sandbox Code Playgroud)

这当然不适用于未连接到移动运营商(iPod Touch,某些iPad等)的设备.因此,作为后退,我将使用或创建自己的IP地理定位 API.您可以在国家/地区级别获得99.5%准确率的免费数据库.这将非常适合检测没有移动提供商的设备的国家/地区.

  • 来自[apple的文档]的@ScottLemmon(http://developer.apple.com/library/ios/DOCUMENTATION/NetworkingInternet/Reference/CTCarrier/Reference/Reference.html#//apple_ref/doc/uid/TP40009596-CH1-SW1) - "如果用户漫游,则值不会更改;它始终代表用户拥有帐户的提供商." (4认同)