user623396
user623396

Reputation: 1547

iOS8 - Get country names from locale

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

Answers (2)

Kalidoss Shanmugam
Kalidoss Shanmugam

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

Pavel Malinnikov
Pavel Malinnikov

Reputation: 359

I have also faced with such issue on iOS 8. Try to use systemLocale instead of currentLocale.

Upvotes: 5

Related Questions