App*_*ect 50 iphone objective-c appsettings application-settings ios
我必须在iPhone设置中获取当前国家/地区.谁能告诉我如何在iPhone应用程序中获取当前国家/地区.
我必须使用当前国家来解析我需要通过当前国家/地区的RSS源.
请帮我找到这个国家.
提前致谢.
ken*_*ytm 137
要查找用户所选语言的国家/地区:
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
// get country code, e.g. ES (Spain), FR (France), etc.
Run Code Online (Sandbox Code Playgroud)
在Swift中:
let currentLocale = NSLocale.currentLocale()
let countryCode = currentLocale.objectForKey(NSLocaleCountryCode) as? String
Run Code Online (Sandbox Code Playgroud)
如果要查找当前时区的国家/地区代码,请参阅@ chings228的答案.
如果要查找设备物理位置的国家/地区代码,则需要使用反向地理编码的CoreLocation.有关详细信息,请参阅此问题:如何从iOS中获取用户的当前位置
小智 17
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
来源链接.
小智 9
对于使用SIM卡的设备,您可以使用:
CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
CTCarrier * carrier = network_Info.subscriberCellularProvider;
NSString * countryCode = [carrier.isoCountryCode uppercaseString];
Run Code Online (Sandbox Code Playgroud)
NSTimeZone *gmt = [NSTimeZone localTimeZone];
NSLog(@"timezone gmt %@",gmt.abbreviation);
Run Code Online (Sandbox Code Playgroud)
如果您正在测试并希望使用与系统不同的语言,则最简单的方法是更改您的方案Application Language
和Application Region
选项,而不是更改设备或模拟器上的语言和区域.
转到Product
- > Scheme
- > Edit Scheme...
- > Run
- > Options
并选择Application Language
和Application Region
你想测试.
从iOS文档:测试您的国际化应用程序
归档时间: |
|
查看次数: |
45792 次 |
最近记录: |