Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Locales on iOS 5

I have this piece of code that gets a few information (current country, country name) from NSLocale.

It works without any problem in iOS 4.3 but crashes in iOS 5.

Upon checking it seems that [locale localeIdentifier] and [locale displayNameForKey: value:] does not work at all but no warnings and errors were detected while building it.

What can I do to get it working in iOS 5?

// Create a pool for autoreleased objects

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Get the current country and locale information of the user
    NSLocale *locale = [NSLocale currentLocale];
    NSString *currentLocaleID = [locale localeIdentifier];    //returns en_US instead of the actual country stored in phone settings
    NSDictionary *localeDictionary = [NSLocale componentsFromLocaleIdentifier:currentLocaleID];     
    NSString *currentCountry = [locale displayNameForKey:NSLocaleCountryCode 
                                                   value:[localeDictionary objectForKey:NSLocaleCountryCode]];     // returns nil

    // Get the list of country codes
    NSArray *countryCodeArray = [NSLocale ISOCountryCodes];
    NSMutableArray *sortedCountryNameArray = [NSMutableArray array];


    for (NSString *countryCode in countryCodeArray) {
        NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode];   //displayNameString is nil after executing this line
        [sortedCountryNameArray addObject:displayNameString];   //app crashes here
    }

    // Drain the autoreleased pool
    [pool drain];
like image 516
user829227 Avatar asked Jul 05 '11 08:07

user829227


1 Answers

This does not crash in iOS 5.1 or 5.1.1

like image 60
lemnar Avatar answered Oct 28 '22 17:10

lemnar