Search code examples
iosswifttemperature

Determine users desired temperature units (F/C) from user settings in SwiftUI


Does anyone know the current best method to determine the state of the users temperature preference settings? The one under Settings -> General -> Language & Region -> Temperature Unit?

I've seen some older answers but they don't work for me. I'm thinking that the method has changed over the last several years.


Solution

  • 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"