56

I have to get the current country in the iPhone settings. Can anyone tell me how to get the current country in iPhone application.

I have to use the current country for parsing the RSS feed in which I need to pass the current country.

Please help me out to find the country .

Thanks in advance.

lukas
  • 2,300
  • 6
  • 28
  • 41
AppAspect
  • 4,461
  • 2
  • 30
  • 46
  • 3
    Is that the country from the localisation settings of the iPhone or the country in which the iPhone is currently physically located? – JeremyP Oct 15 '10 at 09:10

9 Answers9

143

To find the country of the user's chosen language:

NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
// get country code, e.g. ES (Spain), FR (France), etc.

In Swift:

let currentLocale = NSLocale.currentLocale()
let countryCode = currentLocale.objectForKey(NSLocaleCountryCode) as? String

If you want to find the country code of the current timezone, see @chings228's answer.

If you want to find the country code of the device's physical location, you will need CoreLocation with reverse geocoding. See this question for detail: How can I get current location from user in iOS

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 1
    @ranjeet: You can find the reference for NSLocale in http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSLocale_Class/Reference/Reference.html. – kennytm Oct 15 '10 at 08:46
  • 23
    Note! This is ONLY helpful if the person using the phone has actually set the phone to use the locale of their country. That is NOT necessarily the case, and is especially not useful if a person might be on vacation and you still need to detect the exact country they're in. – Ivan Vučica Jul 11 '12 at 14:41
  • 4
    I use xcode 6 and iOS Simulator (iPhone 6/iOS 8.1). I always get US as the countryCode, no matter what language and region I set the iOS simulator to. Also, the output of [[NSLocale preferredLanguage] objectAtIndex:0] is always en. How do I know the code is working correctly? – Unplug Dec 29 '14 at 17:02
  • NSLocale getting Country but, even if we moved to other country it won't get , it reading from phone region. – Vineesh TP Jul 29 '15 at 10:22
  • @VineeshTP You need CoreLocation for that. – kennytm Jul 29 '15 at 14:01
  • This answer gives you the user's locale, but has nothing to do with his current location. E.g. someone from France visiting the United States will still have a French locale. – ruralcoder May 13 '16 at 18:47
  • @Unplug I think on simulator it takes your mac locale (not the simulator locale). Try to change mac locale and see if you get correct values. – user1264176 Feb 17 '17 at 14:39
  • I am using your code. it's working good. But I have an issue, one of my user using an iPhone brought from the US, and he is using that device in India. When running your code the country is shown as the US. I want to show it as India. Please helpme for this. – Vinu Jacob Aug 28 '18 at 08:45
  • @VinuJacob Please check the other two posts linked from the answer. – kennytm Aug 28 '18 at 17:12
18
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
  • The code snippet was working fine but suddenly started giving Exc_Bad_Access error at last line .. !! =( NSZombie is unable to detect it and app crashes. – atastrophic Jul 03 '12 at 10:36
14

For devices with SIM card you can use this:

  CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
  CTCarrier * carrier = network_Info.subscriberCellularProvider;
  NSString  * countryCode = [carrier.isoCountryCode uppercaseString];
user2248258
  • 161
  • 2
  • 5
13

For Swift 4.0,

You can simply use

let countryCode = Locale.current.regionCode

print(countryCode)

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81
9
NSLocale *countryLocale = [NSLocale currentLocale];  
NSString *countryCode = [countryLocale objectForKey:NSLocaleCountryCode];
NSString *country = [countryLocale displayNameForKey:NSLocaleCountryCode value:countryCode];
NSLog(@"Country Code:%@ Name:%@", countryCode, country);
//Country Code:IN Name:India

Source Link.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Teja Kumar Bethina
  • 3,486
  • 26
  • 34
6

If you're testing and want to use a different language from your system, it's easiest to change your scheme's Application Language and Application Region options instead of changing the language and region on your device or simulator.

Go to Product->Scheme->Edit Scheme...->Run->Options and choose the Application Language and Application Region you want to test.

Edit Application Language and Application Region within Scheme Run Options

From the iOS Documentation: Testing Your Internationalized App

Heath Borders
  • 30,998
  • 16
  • 147
  • 256
  • If you think this is a bad answer, please leave a comment about why. I'm using this strategy myself, and it works well as far as I know, but if there are troubles with it, I'd love to know about them! – Heath Borders Feb 04 '16 at 17:06
5
NSTimeZone *gmt = [NSTimeZone localTimeZone];

NSLog(@"timezone gmt %@",gmt.abbreviation);
chings228
  • 1,859
  • 24
  • 24
4

Swift 3.0 latest working code is

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Muhammad Nayab
  • 1,612
  • 14
  • 14
3

Swift 3.0 solution

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
            print(countryCode)
        }
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78