Is there a built in mechanism for Miles/KM interchangeability for iPhone?
I am currently working on a map-based iPhone application and wish to display some information to the user. For my application, it makes sense to have a setting of some sort where the user can choose Miles or Kilometers. Is there a built in mechanism (maybe similar to string localization) for doing this kind of value switching so that I can avoid 开发者_开发知识库an if-block around each time I want to display something to the user?
Take a look at NSLocale. You should be able to get the current locale using [NSLocale currentLocale]. Then call [theLocale objectForKey: NSLocaleMeasurementSystem] and look at the results which should tell you if the users locale uses the metric system. The docs for NSLocale have a constants section which list all of the values that can be passed to the locale.
Using this you would make your own function that could be used in your program to return your distance in a locale specific way.
Based on my quick test NSLocaleUsesMetricSystem
may not work for finding if the users' region uses kilometers or miles.
In the U.K, the distances are in Miles and speed in Miles per hour but NSLocaleUsesMetricSystem
returns 1.
In the U.S, NSLocaleUsesMetricSystem
returns 0 which is correct (distance is in Miles and speed in Miles per hour).
In India, NSLocaleUsesMetricSystem
returns 1 which is correct again (distance is in Kilometers and spee in kph).
精彩评论