Find current country from iPhone device

2019-01-12 20:23发布

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.

9条回答
欢心
2楼-- · 2019-01-12 21:01

For devices with SIM card you can use this:

  CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
  CTCarrier * carrier = network_Info.subscriberCellularProvider;
  NSString  * countryCode = [carrier.isoCountryCode uppercaseString];
查看更多
对你真心纯属浪费
3楼-- · 2019-01-12 21:04
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.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-12 21:08

For Swift 4.0,

You can simply use

let countryCode = Locale.current.regionCode

print(countryCode)

查看更多
Viruses.
5楼-- · 2019-01-12 21:11

Swift 3.0 solution

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
            print(countryCode)
        }
查看更多
爷的心禁止访问
6楼-- · 2019-01-12 21:13

Swift 3.0 latest working code is

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}
查看更多
走好不送
7楼-- · 2019-01-12 21:15

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

查看更多
登录 后发表回答