Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user preferred temperature setting in macOS

I'm trying to read the user set system preferences for Temperature unit (Celsius/Fahrenheit). I was trying to get this data using NSLocale but I cannot find any evidence of a temperature setting in there.

Is it even possible to read this data?

Thanks!

like image 718
Mike Nathas Avatar asked Mar 09 '23 10:03

Mike Nathas


1 Answers

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;
}
like image 73
Code Different Avatar answered Mar 10 '23 22:03

Code Different