Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine user's "Temperature Unit" setting on iOS 10 (Celsius / Fahrenheit)

Tags:

ios

ios10

locale

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.)

like image 694
Tim Norman Avatar asked Sep 27 '16 14:09

Tim Norman


People also ask

How do I change Celsius to Fahrenheit on iOS?

Go to Settings > General > Language & Region. Select Temperature Unit and then change it to either Celsius or Fahrenheit, whichever you want to use. Now your iPhone will use that unit by default whenever it needs to show the temperature.

How do you change units of temperature?

A change of 1 degree Fahrenheit equals a change of 5/9 = 0.56 degrees Celsius. To convert a Fahrenheit temperature to Celsius, subtract 32 and multiply by 5/9. This is the Fahrenheit analog of the Kelvin scale.

How do I change my weather widget from Celsius to iOS?

Go to Day One > Settings > Advanced > toggle the option for Fahrenheit. Turning this off will enable Celsius. You can also change the weather service used in Day One iOS.


1 Answers

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.

like image 102
leopic Avatar answered Sep 30 '22 19:09

leopic