在 iOS 5 上获取区域设置
我有这段代码可以从 NSLocale 获取一些信息(当前国家/地区、国家/地区名称)。
它在 iOS 4.3 中工作没有任何问题,但在 iOS 5 中崩溃。
经过检查,似乎 [locale localeIdentifier] 和 [locale displayNameForKey: value:] 根本不起作用,但在构建它时没有检测到警告和错误。
我该怎么做才能让它在 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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在 iOS 5.1 或 5.1.1 中不会崩溃
This does not crash in iOS 5.1 or 5.1.1