2

I am using NSLocale to detect what country my user is right now in. When i want to check if the user is inside the United States

NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];  

if ([countryCode isEqualToString:@"US"]) {
///////
}

Is it the right way to check?

Zakaria
  • 14,892
  • 22
  • 84
  • 125
orthehelper
  • 4,009
  • 10
  • 40
  • 67

2 Answers2

5

NSLocale is only indicative of the user's linguistic and cultural preferences, not their location. If you want to know the user's physical location, you should use the Core Location APIs.

一二三
  • 21,059
  • 11
  • 65
  • 74
2

This is a "yes" and "no" answer.

Yes, this gives you the user's current preferred Locale. And judging from other questions like yours, other people have decided this answer is "good enough".

However, I could be in China with my iPhone and if I were still working with the English language, I believe the countryCode would still return "US".

If you truly want to know the user's current country, you could use CoreLocation and do some reverse geocoding. With the CoreLocation coordinates, you could do look ups like http://ws.geonames.org/findNearby?lat=47.3&lng=9. There's also MKReverseGeocoder (which is deprecated as of iOS 5.0) and GLGeocoder which has a call of – reverseGeocodeLocation:completionHandler: (documentation linked for you).

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215