Xcode 6.1 iOS 8.1 NSLocale displayNameForKey NSLocaleIdentifier returns zero

 - (NSString *) countryNameByCode: (NSString *) countryCode
 {
     NSString * identifier = [NSLocale localeIdentifierFromComponents: @ {NSLocaleCountryCode: countryCode}];
     NSString * countryName = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];

     return countryName;
 }

countryName no. Why?

+16
iso xcode nslocale
Oct 28 '14 at 16:04
source share
5 answers

This is a known Apple issue for the iOS 8.1 simulator - it does not play on 8.1 devices. See the description of the problem from the Xcode 6.1 release notes below:

Localization and keyboard settings (including third-party keyboards) are incorrectly observed by Safari, Maps, and developer apps in iOS 8.1. Simulator. [NSLocale currentLocale] returns en_US , and only English and Emoji keyboards are available. (18418630, 18512161).

See Xcode 6.1 Release Notes for more information.

+20
Nov 05 '14 at 23:42
source share

Please try the code below, it should work. I tried this on my device as well as simulator 8.1

- (NSString *)countryNameByCode:(NSString*)countryCode { return [[NSLocale systemLocale] displayNameForKey:NSLocaleCountryCode value:countryCode]; } 
+7
Dec 10 '14 at 5:53 a.m.
source share

It works for me

 NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:[NSLocale currentLocale].localeIdentifier]; for (AVSpeechSynthesisVoice *voice in [AVSpeechSynthesisVoice speechVoices]) { NSString *languageLocalised = [currentLocale displayNameForKey:NSLocaleIdentifier value:voice.language]; NSLog(@"%@ displayNameForKey %@: %@", currentLocale.localeIdentifier, voice.language, languageLocalised); } 
+1
Dec 23 '14 at 7:31
source share
 - (NSString *)countryNameByCode:(NSString*)countryCode { NSString *identifier = [[NSLocale preferredLanguages] firstObject]; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:identifier]; NSString *countryName = [locale displayNameForKey:NSLocaleIdentifier value:countryCode]; return countryName; } 

I don’t understand why [[NSLocale currentLocale] displayNameForKey...] does not return the country name in iOS 8 , but the code above should solve your problem.

0
Oct 29 '14 at 10:25
source share

NSString * language = [[NSLocale preferredLanguages] objectAtIndex: 0];

0
Apr 11 '15 at 12:30
source share



All Articles