How do I know which is the default measure system (imperial or metric) on 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.
How to find out the default units of measure system (imperial or metric) in Windows / .NET?
There's the IsMetric
property on the RegionInfo
class.
var us = new RegionInfo("US");
Console.WriteLine($"Is {us.Name} Metric: {us.IsMetric}");
var nl = new RegionInfo("NL");
Console.WriteLine($"Is {nl.Name} Metric: {nl.IsMetric}");
Prints:
Is US Metric: False
Is NL Metric: True
Finding the distance, currency, temperature units etc. for a country (iPhone)
You can use NSLocale
to check the currency unit, but for imperial vs. metric you need to make a list yourself.
Oops. You can check for the imperial vs. metric. There is are NSLocaleMeasurementSystem
and NSLocaleUsesMetricSystem
keys.
Changing the weight system used in app based on user selected metrics?
This is going to be one of those answers that SO hates, but you want to go read up on NSMeasurement.
NSMeasurement holds both a value and a Unit, the later of which is the original measurement type. You store all your data in the format that was originally provided - if the user puts in pounds, store a NSMeasurement with 182 pounds. If they put in kg, make one with 90 kg. You can even put in your own Units, like stone.
From then on, always present the data using an NSMeasurementFormatter. You can pass in the output type, which in your case is the global setting you mentioned in your question. This means that no matter what unit they provided, it always comes out properly converted to the one you want, and changing it instantly changes it everywhere.
Its easy to make your own converters for weird units. I made one for decimal inches and feet/inches, so 13.5 inches turns into 1' 1.5".
How do I get the Measurement Units setting from OS X?
Use
BOOL usesMetic = [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];
NSMeasurementFormatter shows Imperial weights but not metric weights?
It all boils down to the following piece of code:
let weight = Measurement(value: 10.0, unit: UnitMass.kilograms)
let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en_GB")
print(formatter.string(from: weight))
The result is an empty String
(""
).
This is an obvious bug. Please, report it.
A simple workaround is using the older MassFormatter
:
//let locale = Locale(identifier: "en_GB")
let locale = Locale(identifier: "en_US")
let weight = Measurement(value: 10.24, unit: UnitMass.pounds)
let formatter = MassFormatter()
formatter.numberFormatter.locale = locale
formatter.numberFormatter.maximumFractionDigits = 2
if locale.usesMetricSystem {
let kgWeight = weight.converted(to: .kilograms)
print(formatter.string(fromValue: kgWeight.value, unit: .kilogram))
} else {
print(formatter.string(fromValue: weight.value, unit: .pound))
}
How do I know the measurement units corresponding to a given locale?
As far as I know, the C standards and GNU gettext do not offer any way to know the measurement units used in a country, there are no third party libraries that do the job and there are no ready to used databases (free or proprietary).
However, according to CIA - The World Factbook:
At this time, only three countries - Burma, Liberia, and the US - have not adopted the International System of Units (SI, or metric system) as their official system of weights and measures.
So it is fairly easy to write your own code, even without a database or third party libraries. You just have to special case three LC_MEASUREMENT
values (or, better, three patterns, as some of these countries have more than a language):
- Burma (language code pattern:
*_MM
) uses the Burmese system; - Liberia (language code pattern:
*_LR
) uses the Imperial system; - United States (language code pattern:
*_US
) use a customary system similar to the Imperial one. - all other countries use the International System of Units.
UPDATE: all of these countries are in the (slow) process of converting to the metric system (SI). This is probably an another reason why nobody has bothered writing some libraries, code snippets or databases.
Find metric Speed, Degree, etc... standard by location
Some operating systems expose an API to find the preferred measurement standard.
- For Android apps, use
LocaleData.MeasurementSystem
- For MacOS, use
CFPreferencesCopyValue
(guide) - For Windows, use
GetLocaleInfo
- For IOS, use
NSLocaleUsesMetricSystem
(guide)
Otherwise, if you know their location, you can default to Imperial in the United States, Liberia or Myanmar and use Metric in other locations:
Regardless, you should still allow users to manually choose their preference.
Related Topics
Error Appstore Connect:Missing Purpose String in Info.Plist File
How to Create an Ibinspectable of Type Enum
Export Compliance in iOS App Submission
Get Selected Index of Uitableview
How to Get Uiviewcontroller of a Uiview's Superview in iOS
Change Size of Uibarbuttonitem (Image) in Swift 3
Xcode: Could Not Inspect the Application Package
Location Update Even When App Is Killed/Terminated
Getting Url of Uiimage Selected from Uiimagepickercontroller
The Identity Used to Sign the Executable Is No Longer Valid
Show Search Bar in Navigation Bar Without Scrolling on iOS 11
How to Add a Toolbar to the Bottom of a Uitableviewcontroller in Storyboards
Removing from Array During Enumeration in Swift
iOS 11 Uisearchbar in Uinavigationbar
Perform Push Segue After an Unwind Segue