Determine user's Temperature Unit setting on iOS 10 (Celsius / Fahrenheit)
There is this article by Alexandre Colucci that I found: http://blog.timac.org/?tag=nslocaletemperatureunit
First, expose the NSLocaleTemperatureUnit NSLocaleKey:
FOUNDATION_EXPORT NSLocaleKey const NSLocaleTemperatureUnit;
Then check the unit with this:
temperatureUnit = [[NSLocale currentLocale] objectForKey:NSLocaleTemperatureUnit]
Or Swift (2.3):
if let temperatureUnit = NSLocale.currentLocale().objectForKey(NSLocaleTemperatureUnit) {
...
}
It will return a string which is either "Celcius" or "Fahrenheit".
But there is an issue: it's not backwards compatible with iOS versions earlier than 10. If you run your app on an iOS 9 or earlier device, you'll get an error "dyld: Symbol not found: _NSLocaleTemperatureUnit" during app startup.
The solution is to use weak linking for the NSLocaleTemperatureUnit variable definition. Like this:
FOUNDATION_EXPORT NSLocaleKey const NSLocaleTemperatureUnit __attribute__((weak_import));
This will let the app pass the dyld checks. But now you will have to check for the OS version before using NSLocaleTemperatureUnit, or your app will crash with an exception.
if #available(iOS 10,*) {
if let temperatureUnit = NSLocale.currentLocale().objectForKey(NSLocaleTemperatureUnit) {
...
}
}
EDIT:
I tried it in my app, but Apple rejected the app for it when I uploaded it to Testflight. So they definitely don't want us to use the setting for our own formatter classes. I find that pretty annoying.
Determine users desired temperature units (F/C) from user settings in SwiftUI
This works on a physical device (it does not in iOS simulator):
let myFormatter = MeasurementFormatter()
let temperature = Measurement(value: 0, unit: UnitTemperature.celsius)
print(myFormatter.string(from: temperature))
If Settings -> General -> Language & Region -> Temperature is set to Fahrenheit it outputs 32°F otherwise 0°C.
If you are interested in the state you should parse the returned string either for "°C" or "°F"
Get user preferred temperature setting in macOS
The official API is documented under the Preferences Utilities:
let key = "AppleTemperatureUnit" as CFString
let domain = "Apple Global Domain" as CFString
if let unit = CFPreferencesCopyValue(key, domain, kCFPreferencesCurrentUser, kCFPreferencesAnyHost) as? String {
print(unit)
} else {
print("Temperature unit not found")
}
If you wonder how I found it, I used the defaults
utility in the Terminal:
> defaults find temperature
Found 1 keys in domain 'Apple Global Domain': {
AppleTemperatureUnit = Fahrenheit;
}
Found 1 keys in domain 'com.apple.ncplugin.weather': {
WPUseMetricTemperatureUnits = 1;
}
How do I add a degree symbol as part of a UILabel in iPhone development?
Ideally, use MeasurementFormatter
:
let measurement = Measurement(value: weatherDataModel.temperature, unit: UnitTemperature.celsius)
let measurementFormatter = MeasurementFormatter()
measurementFormatter.unitStyle = .short
measurementFormatter.numberFormatter.maximumFractionDigits = 0
measurementFormatter.unitOptions = .temperatureWithoutUnit
temperatureLabel.text = measurementFormatter.string(from: measurement)
No need to convert to Int
. The formatter will handle displaying of decimal digits.
As a bonus, you don't have to do the Kelvin to Celsius conversion by yourself:
let measurementInKelvin = Measurement(value: weatherDataModel.temperature, unit: UnitTemperature.kelvin)
let measurement = measurementInKelvin.converted(to: .celsius)
Related Topics
Could Not Cast Value of Type 'Uitableviewcell' to '(Appname).(Customcellname)'
Does Webkit in iOS 11 (Beta) Support Webrtc
Posting Photos to Facebook Fan Page with iOS Sdk
Alamofire with a Self-Signed Certificate/Servertrustpolicy
How to Add an Identifier to Auto Layout Constraints in Interface Builder
How to Pass Information Between Storyboard Segues
Urlresponse Is Not Retrieved After Storing in Cache Using Storecachedresponse
iOS Web Page Errors Over Cellular Data But Not Over Wifi? Recent Change to At&T Cellular Network
How to Create a Uiimage from the Current Graphics Context
Selecting a Word in a Uitextview
How to Remove Border from Segmented Control
Error Itms-90086 Submitting App
How to Connect Multiple Buttons in a Storyboard to a Single Action
Exit Application When Click Button - iOS
Application(...Continue Useractivity...) Method Not Called in iOS 13