我需要获取iPhone设置中的当前国家信息,有人能告诉我如何在iPhone应用程序中获取当前国家吗。
我需要使用当前国家信息解析RSS Feed,在其中需要传递当前国家信息。
请帮忙查找当前国家信息。
先行致谢。
我需要获取iPhone设置中的当前国家信息,有人能告诉我如何在iPhone应用程序中获取当前国家吗。
我需要使用当前国家信息解析RSS Feed,在其中需要传递当前国家信息。
请帮忙查找当前国家信息。
先行致谢。
查找用户选择语言的国家:
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
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
对于带有SIM卡的设备,您可以使用以下方法:
CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
CTCarrier * carrier = network_Info.subscriberCellularProvider;
NSString * countryCode = [carrier.isoCountryCode uppercaseString];
对于Swift 4.0,
您可以简单地使用
let countryCode = Locale.current.regionCode
print(countryCode)
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
源链接。
来自iOS文档:测试您的国际化应用程序
NSTimeZone *gmt = [NSTimeZone localTimeZone];
NSLog(@"timezone gmt %@",gmt.abbreviation);
最新可用的Swift 3.0代码如下:
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
Swift 3.0 解决方案
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
Locale.current.regionCode
呢? - redent84