Question

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??

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top