如何根据当前位置获取国家/地区代码?

时间:2013-10-11 05:37:00

标签: iphone ios ipad core-location

我有获取国家/地区代码的代码。但我希望印度的国家代码如+91。怎么弄?

NSArray *countryCodes = [NSLocale ISOCountryCodes];     
NSMutableArray *countries = [NSMutableArray arrayWithCapacity:[countryCodes count]];
for (NSString *countryCode in [NSLocale ISOCountryCodes])  {    
    NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
    NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
    [countries addObject: country];
}

NSDictionary *codeForCountryDictionary = [[NSDictionary alloc] initWithObjects:countryCodes forKeys:countries];

NSLog(@”%@”,codeForCountryDictionary);

1 个答案:

答案 0 :(得分:3)

您可以将CoreTelephony框架用于此目的。在您的项目中包含此框架

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