How to get user's country information

女生的网名这么多〃 提交于 2019-12-02 08:53:28

问题


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 RootViewController's loading. Is there anyway of doing this in the iPhone SDK??


回答1:


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.



来源:https://stackoverflow.com/questions/2803944/how-to-get-users-country-information

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