views:

298

answers:

1

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

A: 

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.

hjd
Unfortunately the Locale is not good enough as I need the country that the device is located in while the app is running. I had read about that limitation of the MKReverseGeocoder API and that's why I was looking for an alternative. I suppose if I load a map in the background hidden from the user just to get the country info, Apple might reject the app.
ender
hjd
thanx hjd,i'll check it out, though I would prefer to avoid relying on an outside resource because it brings with it a whole new set of scenarios/problems.
ender