54

我必须在 iPhone 设置中获取当前国家/地区。谁能告诉我如何在 iPhone 应用程序中获取当前国家/地区。

我必须使用当前国家/地区来解析我需要传递当前国家/地区的 RSS 提要。

请帮我找出国家。

提前致谢。

4

9 回答 9

141

要查找用户所选语言的国家/地区:

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

在斯威夫特:

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

如果您想查找当前时区的国家代码,请参阅@chings228 的答案

如果您想查找设备物理位置的国家代码,则需要使用反向地理编码的 CoreLocation。有关详细信息,请参阅此问题:How can I get current location from user in iOS

于 2010-10-15T08:32:28.373 回答
18
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
于 2012-05-19T11:21:59.407 回答
13

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

  CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
  CTCarrier * carrier = network_Info.subscriberCellularProvider;
  NSString  * countryCode = [carrier.isoCountryCode uppercaseString];
于 2017-04-06T11:53:46.820 回答
11

对于 Swift 4.0,

你可以简单地使用

let countryCode = Locale.current.regionCode

print(countryCode)

于 2018-08-18T08:42:16.527 回答
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

源链接

于 2015-03-17T10:51:56.323 回答
6

如果您正在测试并希望使用与您的系统不同的语言,最简单的方法是更改​​您的方案Application LanguageApplication Region选项,而不是更改设备或模拟器上的语言和区域。

转到Product-> Scheme-> Edit Scheme...-> Run->Options并选择要测试的Application Language和。Application Region

在方案运行选项中编辑应用程序语言和应用程序区域

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

于 2015-12-11T22:07:20.767 回答
5
NSTimeZone *gmt = [NSTimeZone localTimeZone];

NSLog(@"timezone gmt %@",gmt.abbreviation);
于 2011-12-02T14:36:49.070 回答
4

Swift 3.0 最新的工作代码是

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
    print(countryCode)
}
于 2017-07-07T13:52:45.460 回答
3

Swift 3.0解决方案

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
            print(countryCode)
        }
于 2016-10-19T08:57:16.463 回答