开发者

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 *coun开发者_如何学GotryCode 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];


This does not crash in iOS 5.1 or 5.1.1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜