Reputation: 1547
I'm using this code to get the list of countries:
for (NSString *code in [NSLocale ISOCountryCodes])
{
NSString *identifier = [NSLocale localeIdentifierFromComponents:@{NSLocaleCountryCode: code}];
NSString *countryName = [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:identifier];
NSLog(countryName);
}
This works fine in iOS 7.x but fails in iOS8 (beta 2) - with countryName
always being nil.
Anyone found an alternative yet?
Upvotes: 3
Views: 1049
Reputation: 1
let currentLocale : NSLocale = NSLocale.init(localeIdentifier : NSLocale.current.identifier)
let countryName : String? = currentLocale.displayName(forKey: NSLocale.Key.countryCode, value: countryCode)
print(countryName ?? "Invalid country code")
Note: If you manually entered localIdentifier means IOS 8 not listed all country name (Eg: "en_US")
Upvotes: 0
Reputation: 359
I have also faced with such issue on iOS 8. Try to use systemLocale
instead of currentLocale
.
Upvotes: 5