How to tell user's locale units settings

102 Views Asked by At

I'm in the process of internationalizing my iOS app to handle both US customary units (imperial) and SI (metric). Is there a way to tell what the user's preferred settings are based on their locale or other settings info?

I ask the user to input Measurement() data and need to know what unit to assign it when calling let weight = Measurement(value: 75, unit: UnitMass.kilograms). I would like to be able to assign the correct unit for the user without needing to ask them for their preference. I know that the system can handle the correct output units using weight.formatted(). But what about input?

My app is very user data input oriented using the following UnitTypes:

Measurement      Imperial            Metric

Length           feet                meters
Distance         miles               kilometers
Mass             pounds              kilograms
Displacement     cubicInches         liters
Volume           gallons             liters
Power            horsepower          kilowatts
Temperature      farenheit           celcius
AtmosPressure    inchesOfMercury     millibars
Speed            milesPerHour        kilometersPerHour
1

There are 1 best solutions below

0
Mojtaba Hosseini On

You can use the proper initializer for each unit like:

let currentMassUnit = UnitMass(forLocale: .current)

Note that maybe the user not prefers their locale for the calculations.