14

iOS 10 adds the ability for the user to set their "Temperature Unit" choice under Settings > General > Language & Region > Temperature Unit.

How can my app programmatically determine this setting so it can display the right temperature unit? I poured through NSLocale.h and didn't see anything relevant.

(Before iOS 10, it was sufficient to test if the locale used metric and assume that metric users want to use Celsius. This is no longer the case.)

1
  • Let's start with where this setting is used. I've played around with the NSDimensionFormatter class to format a temperature dimension and it doesn't seem to honor the "Temperature Unit" setting from the Settings app. Have you found anything that changes display based on this setting?
    – rmaddy
    Commented Sep 27, 2016 at 15:03

8 Answers 8

20

There is an (NS)MeasurementFormatter class. It inherits from an (NS)Formatter class. It's a new class available for iOS 10+ SDK.

I am not sure whether it's necessary to know, what unit a user has set in their preferences.

To set a Measurement using Swift 3:

let formatter = MeasurementFormatter()
let measurement = Measurement(value: 24.5, unit: UnitTemperature.celsius)
let temperature = formatter.string(from: measurement)
print(temperature) // 76.1°F 
// this value was computed to Fahrenheit value on my locale/preferences

For retrieval of a Measurement:

print(measurement.unit) // °C - always celsius as it was set as Celsius

formatter.unitStyle = .long

formatter.locale = Locale.current
formatter.string(from: measurement.unit) // degrees Celsius - always an original unit
formatter.string(from: measurement) // 76.1 degrees Fahrenheit - regarding locale/settings

formatter.locale = Locale.init(identifier: "it_IT")
formatter.string(from: measurement.unit) // gradi Celsius - always an original unit
formatter.string(from: measurement) // 24,5 gradi Celsius - regarding locale/settings

The system knows, what unit we have set. It will handle all the value conversion work, because the value was set as a pair of a value and a measurement unit.

For manual conversion:

measurement.converted(to: UnitTemperature.kelvin).value  // 297.65

Swift:

https://developer.apple.com/reference/foundation/measurementformatter

Objective-C:

https://developer.apple.com/reference/foundation/nsmeasurementformatter?language=objc


Feel free to correct a grammar.

7
  • 4
    None of this deals with the question though. MeasurementFormatter does not change its output based on the "Temperature Unit" setting in iOS 10. It formats based on the locale which is not the same thing.
    – rmaddy
    Commented Sep 27, 2016 at 18:23
  • Yes, it doesn't. But after a solid research, I assume, there is no way to get this info by any API and the reason why is that it is not needed though.
    – pedrouan
    Commented Sep 27, 2016 at 18:26
  • Why do you say it isn't needed? If MeasurementFormatter doesn't honor the new "Temperature Unit" setting in the Settings app, then some other way must be found to honor the setting made by the user.
    – rmaddy
    Commented Sep 27, 2016 at 18:27
  • Which one what? I don't know "which one" you are referring to. I am simply pointing out that your answer doesn't actually make any attempt to answer the question being asked. It's good info but it's not related to the question.
    – rmaddy
    Commented Sep 27, 2016 at 18:35
  • 2
    I tested this approach on a device running iOS 10.2 and it worked. This is described by an Apple engineer here forums.developer.apple.com/thread/70258 Commented Feb 9, 2017 at 13:24
5

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.

3
  • According to this (and my tests in iOS 10.2) using the approach @pedroan described works if you test on a device. forums.developer.apple.com/thread/70258 Commented Feb 9, 2017 at 13:24
  • 1
    Apple will definitely not let you use the NSLocaleTemperatureUnit symbol, as that is private. But I do wonder if they will allow the string value, i.e. passing an explicit value of "kCFLocaleTemperatureUnitKey" directly (which is the value for the symbol). That is not referencing any private APIs technically. If not, the same value may also be available from NSUserDefaults under the "AppleTemperatureUnit" key, but Apple could check for that name too, not sure. Commented Nov 3, 2018 at 21:02
  • Oh, and the value it returns is "Celsius", not "Celcius" . Commented Nov 3, 2018 at 21:05
3

I wrote a little extension for UnitTemperature to get the unit selected based on the findings from Fábio Oliveira. My use case was knowing which unit the user had selected, not necessarily using it to display something, this is how I did it.

extension UnitTemperature {
  static var current: UnitTemperature {
    let measureFormatter = MeasurementFormatter()
    let measurement = Measurement(value: 0, unit: UnitTemperature.celsius)
    let output = measureFormatter.string(from: measurement)
    return output == "0°C" ? .celsius : .fahrenheit
  }
}

Again, remember to test this using an actual device, not a Simulator.

5
  • Does this still work if the system language is not set to English? Commented Mar 15, 2020 at 18:18
  • What about kelvin?
    – User
    Commented Aug 2, 2020 at 19:26
  • @Ixx in iOS you can only set the Temperature Unit to either Celsius or Fahrenheit
    – leopic
    Commented Aug 2, 2020 at 19:51
  • No? Measurement(value: 0, unit: UnitTemperature.kelvin)
    – User
    Commented Aug 5, 2020 at 18:40
  • You are right, I meant as a user, you can only set your Temperature Units, in the Settings App, to either Celsius or Fahrenheit. So don't have to worry about Kelvin being one of the possible values the user selected, because it's not an option at all.
    – leopic
    Commented Aug 6, 2020 at 3:34
2

The correct answer is from Fabio's comment to pedrouan's answer, which references Apple's response on the forums: https://forums.developer.apple.com/thread/70258.

The Simulator does not respect the Temperature Unit setting, but real devices do respect that setting. Using MeasurementFormatter in the Simulator will always use the locale's default unit, but using MeasurementFormatter on a real device will use the user's selected Temperature Unit.

1
  • 1
    It's just a big mess, and nobody at Apple is cleaning it up. Boo to them. I need to know to which unit the user has set it. Not for screen formatting purposes, but for other processing purposes. There's just no way to do that. Except to convert some known value with measurementformatter, and check it against known strings (e.g. 'C') for all languages. What a failure.
    – RetepV
    Commented May 6, 2020 at 8:41
1

This seems to be a late answer, but I found a neat solution mentioned by @fábio-oliveira in the comments here.

The original Apple thread: https://forums.developer.apple.com/thread/70258

What I'm trying to do in my solution is to have a function to convert Kelvin temperature (parsed from JSON file and can be any temperature) into a temperature format dictated by Systems Locale or set by a user in Settings > General > Langauge & Region > Temperature Unit. However, I also need the temperature to display 0 digits after the decimal separator.

So here's a Swift 4 solution (kelvin is the input temperature unit):

func temperatureFormatter(kelvinTemp: Double) -> String{
        let mf = MeasurementFormatter()
        mf.numberFormatter.maximumFractionDigits = 0
        let t = Measurement(value: kelvinTemp, unit: UnitTemperature.kelvin)
        return (String(format:"%@", mf.string(from: t)))
    }

And here you can call your function to convert the values (_currentTemp is your extracted JSON temperature value & convertedTemp is your resulting converted temperature based on conditions mentioned above):

convertedTemp = temperatureFormatter(kelvinTemp: _currentTemp)

Let me know if this solution works for you, it does work for me though. Thank you!

1

I ended up doing something similar to Amir but a little more robust, i.e. Amir's solution was failing for me at times.

Instead of instantiating via Self(symbol:) I match the symbol returned when formatting against the three options:

extension UnitTemperature {
  static var current: UnitTemperature {
    let formatter = MeasurementFormatter()
    formatter.locale = Locale.autoupdatingCurrent
    formatter.unitStyle = .medium
    let formatted = formatter.string(from: .init(value: 0, unit: UnitTemperature.celsius))
    let symbol = String(formatted.suffix(2))
    switch (symbol) {
      case UnitTemperature.celsius.symbol:
        return .celsius
      case UnitTemperature.fahrenheit.symbol:
        return .fahrenheit
      default:
        return .kelvin
    }
  }
}
0

As others mentioned, the safe way is using MeasurementFormatter directly for formatting if you need to know the current unit! I made this extension, which works in Swift 5 and newer:

extension UnitTemperature {
    static var current: Self {
        let formatter = MeasurementFormatter()
        formatter.locale = .init(identifier: "en_US")
        formatter.unitStyle = .long
        let formatted = formatter.string(from: .init(value: 0, unit: Self.celsius))
        return Self(symbol: formatted.components(separatedBy: " ").last!)
    }
}

You can use it like:

UnitTemperature.current

Note: it may not work on simulator, it works on device

0

Just compare the system locale string with a known one. like this:

static public func getDefaultTemperatureUnits() -> UserPreferences.TemperatureUnits {
    let locale = Locale.current
    let formatter = MeasurementFormatter()
    let measurement = Measurement(value: 37.0, unit: UnitTemperature.celsius)
    formatter.unitStyle = .medium

    formatter.locale = Locale.current
    let localeTemperature = formatter.string(from: measurement).lowercased()
    
    let formatter2 = MeasurementFormatter()
    let numberFormatter = NumberFormatter()
    numberFormatter.maximumFractionDigits = 0
    formatter2.numberFormatter = numberFormatter
    formatter2.unitOptions = [.providedUnit]
    formatter2.unitStyle = .medium
    let temperature = formatter2.string(from: measurement.converted(to: UnitTemperature.celsius))
    let knownCelsiusTemperature = temperature.lowercased()
    
    return knownCelsiusTemperature == localeTemperature ? .celsius : .fahrenheit
}

This is good for all languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.