Determine User's "Temperature Unit" Setting on iOS 10 (Celsius/Fahrenheit)

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



Leave a reply



Submit