Search code examples
swiftxcodecocoafoundationtemperature

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!


Solution

  • 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;
    }