NSLocale returning wrong value in iOS 11

爱⌒轻易说出口 提交于 2019-12-19 03:24:19

问题


Starting from iOS 11.0, the following code returns "de_US" instead of "en_US":

// => Returns "de_US"
NSString *regionCode = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
NSLog(@"Region code: %@", regionCode);

Below iOS 11, it returns "en_US".

My device has for language and region English / United States. Preferred languages (despite I do not use them in my code) are in order:

  1. English
  2. Deutsch
  3. French

Is it a known issue of iOS 11? Has the API changed?


回答1:


Found it!

It's a change behaviour starting from iOS 11.

Under iOS 11, [NSLocale currentLocale] only returns languages supported by your app’s localizations. If your app only supports English (as the base localization), then no matter what language the user selects on the device, currentLocale will always return English.

Under iOS 10 and earlier, currentLocale would directly represent the user’s chosen language and region, regardless of what localizations your app supports.

More information here:

  • https://jaanus.com/ios-11-changes-localized-date-handling/

  • Maddy's stack overflow answer: https://stackoverflow.com/a/46206511/1226963



来源:https://stackoverflow.com/questions/47676897/nslocale-returning-wrong-value-in-ios-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!