我使用的是从美国购买的iphone5s,并使用印度运营商在印度使用iphone5进行开发。我正在尝试使用NSLocale
获取国家代码,但这给了我US
而不是IN
。
我应该怎么做才能使其成为IN
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
NSLog(@"country code %@",countryCode); //US
最佳答案
NSLocale
的currentLocale
将为您提供有关设备设置(语言和地区)上设置的语言环境的信息。
如果要获取承运人的国家/地区代码,则必须使用CoreTelephony
框架:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
...
CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;
需要注意的几件事:
如果存在以下任一情况,则此属性的值(isoCountryCode)为nil:
More info on the docs here
关于ios - iOS获取国家代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31905258/