Find current country from iPhone device

前端 未结 9 881
醉话见心
醉话见心 2020-12-04 08:38

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

相关标签:
9条回答
  • 2020-12-04 09:15
    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.

    0 讨论(0)
  • 2020-12-04 09:22

    Swift 3.0 latest working code is

    if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
        print(countryCode)
    }
    
    0 讨论(0)
  • 2020-12-04 09:23

    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

    0 讨论(0)
提交回复
热议问题