开发者

How to get user's country information

开发者 https://www.devze.com 2022-12-29 15:26 出处:网络
Basically I need to identify the user\'s country at application startup and enable or disable a location based feature in my app based on his/her country.I need to do this as quickly as possible in th

Basically I need to identify the user's country at application startup and enable or disable a location based feature in my app based on his/her country. I need to do this as quickly as possible in the application delegate prior to the RootViewCon开发者_StackOverflow社区troller's loading. Is there anyway of doing this in the iPhone SDK??


Do you need the name of the country that the device is physically located in while the app is running?

Or could you make do with the system locale the device is using such as en_US or fr_CA ?

If the latter, you can:

NSLocale *currentUsersLocale = [NSLocale currentLocale];  
NSString *currentLocaleID = [currentUsersLocale localeIdentifier];`

Then currentLocaleID will be "en_US" or "fr_CA" or similar.

Otherwise you'll need to use CoreLocation and some form of Reverse Geocoder to convert your lat/long into a country name.

MKReverseGeocoder can do this but the terms of service say you need to be doing it in conjunction with a Google map which may not suit you. Only available on OS 3.0 and above, search the documentation for MKReverseGeocoder for more info.

I needed this myself and ended up doing Geocoding in PHP on our server, using the device IP address. It's rudimentary, but gave us enough detail for our needs.

Check out this web service: http://ws.geonames.org/findNearby?lat=47.3&lng=9
Which returns an XML doc with country and nearby place names given a lat/lng pair.

That approach relies on the device having an active data connection though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号