From e8459e19b41f06ad80e76c25c46fbe5853c4e38e Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 12 Mar 2024 15:07:24 +0100 Subject: [PATCH] [resources] Fix regionCode crash on iOS before 17 (#4473) The regionCode API ``` NSLocale.currentLocale().regionCode ``` was published in iOS 17: https://developer.apple.com/documentation/foundation/nslocale/4172868-regioncode?language=objc to make it works on all iOS versions we have to use: ``` NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as? String ``` fixes https://github.com/JetBrains/compose-multiplatform/issues/4469 --- .../jetbrains/compose/resources/ResourceEnvironment.ios.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.ios.kt b/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.ios.kt index b941d0ff10..6fd9d908e9 100644 --- a/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.ios.kt +++ b/components/resources/library/src/iosMain/kotlin/org/jetbrains/compose/resources/ResourceEnvironment.ios.kt @@ -8,14 +8,16 @@ import platform.UIKit.UIUserInterfaceStyle internal actual fun getSystemEnvironment(): ResourceEnvironment { val locale = NSLocale.currentLocale() + val languageCode = locale.languageCode + val regionCode = locale.objectForKey(NSLocaleCountryCode) as? String val mainScreen = UIScreen.mainScreen val isDarkTheme = mainScreen.traitCollection().userInterfaceStyle == UIUserInterfaceStyle.UIUserInterfaceStyleDark //there is no an API to get a physical screen size and calculate a real DPI val density = mainScreen.scale.toFloat() return ResourceEnvironment( - language = LanguageQualifier(locale.languageCode), - region = RegionQualifier(locale.regionCode.orEmpty()), + language = LanguageQualifier(languageCode), + region = RegionQualifier(regionCode.orEmpty()), theme = ThemeQualifier.selectByValue(isDarkTheme), density = DensityQualifier.selectByDensity(density) )