views:

888

answers:

5

I need to determine at startup what country a user is in, for an iPhone application. Presumably I will have to turn on location services and do some sort of reverse geocoding. I don't really want to use a third party web service if possible, are there any other suggestions for determining country from what location services provides?

Initially, I only need to check whether the user is inside the US or not, but this may change in the future to add more countries. I understand that location can not always be determined or the user may have location services turned off. Basically, I just need to know if the user is detected as being inside the US, in order to turn off a specific feature.

EDIT: On further reading, it looks like MKReverseGeocoder would be the way to go, except I don't wish to display any maps in my app, which means I'm not allowed to use this.

A: 

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 http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test

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

Krumelur
The purpose is to restrict functionality if the user is found to be within the US. If the user has location services turned off or they are found to be outside the US, functionality is not restricted. I know it's easy to get around, but these are the requirements I have - it's more a preference that functionality is restricted rather than a 100% every time requirement.
alku83
I certainly would be happy to use the Google services provided by the iPhone SDK. Is there something which will give me what I'm after?
alku83
+1  A: 

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];

4.0 only, though.

zoul
Would not work with iPod Touch, missing telephony
JOM
Of course, it’s just an additional source of information.
zoul
+1  A: 

The an NSLocale object (such as returned by [NSLocale systemLocale] and [NSLocale audoupdatingCurrentLocale])contains the value NSLocaleCountryCode. Check out in Apple's documentation.

Drew C
Surely the user could just change their locale?
alku83
A: 

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.

alku83
+2  A: 
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];

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