Determine iPhone user's country

风流意气都作罢 提交于 2019-11-27 19:03:36

Another trick you can try is checking the carrier’s MCC:

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

CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netInfo subscriberCellularProvider];
NSString *mcc = [carrier mobileCountryCode];
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

NSString *countryName = [locale displayNameForKey: NSLocaleCountryCode
                         value: countryCode];

Given the restrictions of MKReverseGeocoder, it seems the only feasible way for me to achieve what I am after is to use a third party service to perform a reverse geocode. I have chosen to go with GeoNames as they seem to be the standard choice.

Drew C

The NSLocale object, such as returned by [NSLocale systemLocale], and[NSLocale autoupdatingCurrentLocale]`) contains the value NSLocaleCountryCode. Check it out in the Apple documentation.

Krumelur

If you don't want to use the Google services provided by the iPhone SDK, couldn't you just store the coordinates of the US of A borders and check whether or not you are inside that?

Here is a relevant question in that case How can I determine whether a 2D Point is within a Polygon?

If the purpose of the limitation is something other than user experience (for example, to enforce complicance with some specific US law), i.e. when the user can not be trused, I would say that you need some more rigorous checking (after all, the user would simply disallow the use of location services otherwise, wouldn't he/she?).

One such approach would be to do an IP lookup, e.g. http://www.maxmind.com/app/geoip_country

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!