How do I know which is the default measure system (imperial or metric) on iOS?

IosUnits of-Measurement

Ios Problem Overview


How do I know which is the default measure system (imperial or metric) on iOS ?

How do I get this preference from the device settings, so I know what to display in my app ?

thanks

Ios Solutions


Solution 1 - Ios

The NSLocale can tell you:

NSLocale *locale = [NSLocale currentLocale]; 
BOOL isMetric = [[locale objectForKey:NSLocaleUsesMetricSystem] boolValue];

Only three countries do not use the metric system: the US, Liberia and Myanmar. The later uses its own system, the former two use Imperial Units.

Apples documentation says (emphasis mine):

> NSLocaleUsesMetricSystem

>The key for the flag that indicates whether the locale uses the metric system. The corresponding value is a Boolean NSNumber object. If the value is NO, you can typically assume American measurement units (for example, the statute mile).

>Available in iOS 2.0 and later.

Solution 2 - Ios

@DarkDust answer for swift3

//User region setting return
let locale = Locale.current //NSLocale.current

//Returns true if the locale uses the metric system 
let isMetric = locale.usesMetricSystem

Solution 3 - Ios

here's a swift version

var locale = NSLocale.currentLocale()
let isMetric = locale.objectForKey(NSLocaleUsesMetricSystem) as! Bool

Solution 4 - Ios

For swift 3

    let locale = NSLocale.current
    let isMetric = locale.usesMetricSystem

Solution 5 - Ios

As others mentioned before, the UK uses a mix of metric and imperial units.

I would recommend using the new MeassurementFormatter introduced in iOS 10 which handles most of these discrepancies:

import Foundation

let locale = Locale(identifier: "EN_UK")
locale.usesMetricSystem // true!
var formatter = MeasurementFormatter()
formatter.locale = locale
formatter.string(from: Measurement(value: 1000, unit: UnitLength.meters)) // 0.621 mi

To render a distance as a string in local, natural unit, use:

let distanceInMeters: CLLocationDistance = 1000
let formatter = MeasurementFormatter()
formatter.string(from: Measurement(value: distanceInMeters, unit: UnitLength.meters)) // 0.621 mi

Official documentation: https://developer.apple.com/documentation/foundation/measurementformatter

Solution 6 - Ios

You should probably just have a setting in your app and let your users choose -- this is what Apple does in the Weather app.

If you want to choose a sensible default you could look at the locale. If it's US, pick imperial otherwise choose metric. It is a heuristic, it will be wrong sometimes, but it's just a default that can be changed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaneuryzmView Question on Stackoverflow
Solution 1 - IosDarkDustView Answer on Stackoverflow
Solution 2 - IosNazmul HasanView Answer on Stackoverflow
Solution 3 - IosnibtyView Answer on Stackoverflow
Solution 4 - IosGIJOWView Answer on Stackoverflow
Solution 5 - IosEneko AlonsoView Answer on Stackoverflow
Solution 6 - IosStephen DarlingtonView Answer on Stackoverflow