从iPhone设备中查找当前国家

56

我需要获取iPhone设置中的当前国家信息,有人能告诉我如何在iPhone应用程序中获取当前国家吗。

我需要使用当前国家信息解析RSS Feed,在其中需要传递当前国家信息。

请帮忙查找当前国家信息。

先行致谢。


3
这是指 iPhone 的本地化设置所在的国家,还是 iPhone 当前物理位置所在的国家? - JeremyP
9个回答

143

查找用户选择语言的国家:

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

在Swift中:

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

如果您想找到当前时区的国家代码,请参阅@chings228的答案
如果您想找到设备物理位置的国家代码,则需要使用CoreLocation和反向地理编码。有关详细信息,请参见此问题:如何在iOS中从用户获取当前位置

1
@ranjeet:你可以在http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSLocale_Class/Reference/Reference.html找到NSLocale的参考资料。 - kennytm
23
注意!只有当使用手机的人已经将手机语言设置为其所在国家/地区的语言时,本方法才有用。这并不一定是情况,尤其是如果一个人可能正在度假,仍需要检测他们所在的确切国家/地区,则本方法无法提供帮助。 - Ivan Vučica
4
我使用的是Xcode 6和iOS模拟器(iPhone 6/iOS 8.1)。无论我将iOS模拟器设置成什么语言和地区,我总是得到US作为countryCode。另外,[[NSLocale preferredLanguage] objectAtIndex:0]的输出始终为en。我如何确定代码是否正常工作? - Unplug
NSLocale获取国家,但即使我们移居到其他国家,它也不会得到正确的结果,因为它是从手机地区读取的。 - Vineesh TP
@VineeshTP 你需要使用CoreLocation。 - kennytm
显示剩余6条评论

18
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];

代码片段之前一直运行良好,但突然在最后一行出现了Exc_Bad_Access错误.. !! =( NSZombie无法检测到它并导致应用程序崩溃。 - atastrophic

14

对于带有SIM卡的设备,您可以使用以下方法:

  CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
  CTCarrier * carrier = network_Info.subscriberCellularProvider;
  NSString  * countryCode = [carrier.isoCountryCode uppercaseString];

13

对于Swift 4.0,

您可以简单地使用

let countryCode = Locale.current.regionCode

print(countryCode)


如果我移居到其他国家,那么它会返回什么? - Ekta Padaliya

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

源链接


应该是NSLog(@"国家代码:%@ 名称:%@", countryCode, country); - AlexanderZ

7
如果您正在进行测试,并且想要使用与您的系统不同的语言,最简单的方法是更改方案的“应用程序语言”和“应用程序区域”选项,而不是更改设备或模拟器上的语言和区域。
前往“Product” ->“Scheme” ->“Edit Scheme ...” ->“Run” ->“Options”,并选择您要测试的“应用程序语言”和“应用程序区域”。

Edit Application Language and Application Region within Scheme Run Options

来自iOS文档:测试您的国际化应用程序


如果您认为这个答案不好,请留下评论说明原因。我自己正在使用这种策略,就我所知它运作良好,但如果有问题的话,我很乐意了解! - Heath Borders

5
NSTimeZone *gmt = [NSTimeZone localTimeZone];

NSLog(@"timezone gmt %@",gmt.abbreviation);

4

最新可用的Swift 3.0代码如下:

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}

3

Swift 3.0 解决方案

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
            print(countryCode)
        }

8
为什么不直接使用Locale.current.regionCode呢? - redent84

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接