Is there a built-in Miles / KM interchangeability mechanism for iPhone?

I am currently working on map-based applications and want to display some information for the user. For my application, it makes sense to have some kind of setting where the user can select Miles or Kilometers. Is there a built-in mechanism (possibly similar to string localization) for switching values ​​so that I can avoid the if-block every time I want to display something to the user?

+6
objective-c iphone localization
source share
2 answers

Take a look at NSLocale . You can get the current locale using [NSLocale currentLocale]. Then call [theLocale objectForKey: NSLocaleMeasurementSystem] and look at the results, which should tell you if the user's locale uses the metric system. Documents for NSLocale have a constant section that lists all the values ​​that can be passed to the locale.

Using this, you will make your own function that can be used in your program to return your distance in a particular language.

+12
source share

Based on my quick test, the NSLocaleUsesMetricSystem may not work if you find that the user region is using kilometers or miles.

In the UK, the distances are in Miles and the speed is in Miles per hour, but NSLocaleUsesMetricSystem returns 1.

In the US, NSLocaleUsesMetricSystem returns 0, which is correct (distance is in Miles and speed is in Miles per hour).

In India, the NSLocaleUsesMetricSystem returns 1, which is again correct (distance is in kilometers and indicated in km / h).

-one
source share

All Articles