Q: Find temperature unit from settings app I am working on an iOS app in Swift 3, I need to get user preferred unit for temperature from settings app (Celsius or Fahrenheit). I tried finding a solution for it but no success. :(
Can anyone suggest how to do this.
Thanks in advance!

A: This can be done with the MeasurementFormatter in swift.
This method does not use any private API's. Remember to run this on an actual device(iOS 10+) as the simulator does not have that option.
    let formatter = MeasurementFormatter.init()
    let temperature = Measurement.init(value: 37.0, unit: UnitTemperature.celsius)
    let localTemperature = formatter.string(from: temperature)
    if(localTemperature.contains("C")) {
        print("UNIT IS °C")
    } else {
        print("UNIT IS °F")
    }


A: The kCFLocaleTemperatureUnitKey key is not documented. So apple might have problem with that. Please read this thread for more. His app was rejected because of this key.
let locale = NSLocale.current as NSLocale
let obj = locale.object(forKey: NSLocale.Key(rawValue: "kCFLocaleTemperatureUnitKey"))
print("\(obj!)")


A: You need to expose NSLocaleTemperatureUnit of type NSLocaleKey, 
FOUNDATION_EXPORT NSLocaleKey const NSLocaleTemperatureUnit;

Then you will be able to access NSLocale information based on the temperature unit NSLocaleKey.
NSLocale *locale = [NSLocale currentLocale];
__unused id info = [locale objectForKey:NSLocaleTemperatureUnit];

For example


A: I've modified other answers for more elegant option
extension Locale {

    var temperatureUnit: UnitTemperature {
        let units: [UnitTemperature] = [.celsius, .fahrenheit, .kelvin]

        let measurement = Measurement(value: 37, unit: UnitTemperature.celsius)

        let temperatureString = MeasurementFormatter().string(from: measurement)

        let matchedUnit = units.first { temperatureString.contains($0.symbol) }
        if matchedUnit != nil {
            return matchedUnit!
        }
        
        return usesMetricSystem ? .celsius : .fahrenheit
    }
}

