NSLocaleUsesMetricSystem always YES on iPad - ios

I am trying to decide if the users Region/Locale settings validates the use of Metric values (in this case if kg or lb should be appended).
I am running 3.2 on my iPad and on the simulator(Xcode 3.2.4).
I have tried out a few different Region settings but I simply can not make it return NO for NSLocaleUsesMetricSystem
NSLocale *locale = [NSLocale systemLocale];
BOOL usesMetric = [[locale objectForKey:NSLocaleUsesMetricSystem] boolValue];
NSLog(#"The system is Metric: %#\n", (usesMetric ? #"YES" : #"NO"));
This even happens for language set to English and region set to United States on both the simulator and on the actual device.
I also tried NSLocaleMeasurementSystem and it too always returns "Metric", never "U.S.".
How will I go about deciding if I should use Metric or not?
Thanks for any help given:)
...could I be so lucky that the whole world changed to metric while I was sleeping:)

Try currentLocale instead of systemLocale

Had the same problem, until I realized, that UK officially uses metric system. I have always thought that they still use imperial, so I tested with UK.
As soon as I started to test US locale, NSLocaleUsesMetricSystem started to return "NO" as expected.
Hope this helps.

Related

Changing the temperature in my app based on the users preferences

Right now my app just displays the temperature in Fahrenheit. I would like it so that if the user in their settings (General > Language & Region > Temperature Unit) has it set to Celsius my app would then precede to show that. Sadly I cant think of a way to fix this bug... It seems that I am just stuck on Fahrenheit. Does anyone have any suggestions? Thanks everyone :)
Here you go, I am not sure if there is a way to know which temperature system the user prefers, but there is a way to know if he use the metric system (Celsius) or imperial (Fahrenheit).
let locale = Locale.current
let isMetric = locale.usesMetricSystem
if isMetric {
// Use Celsius
} else {
// Use Fahrenheit
}

Determine country code for iOS Region "Europe"

I am working on an app which asks users for phone number and has a country picker, where the user should input country code.
But while testing null country code values, I found out that somehow my iPhone with iOS 10.2.1 has a region format set to Europe.
The Europe region does not exist in the regions list when I search for it.
When I try to get the current locale country code
NSLocale *currentLocale = [NSLocale currentLocale];
return [currentLocale objectForKey:NSLocaleCountryCode]
I get en_150 which is not useful to determine user's country, at least on my device. Returned country code is 150, which does not exist.
I found a workaround to find to country code by using CTTelephonyNetworkInfo
if(![currentLocale objectForKey:NSLocaleCountryCode]) {
CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;
return [countryCode capitalizedString];
}
But how does it work with this Europe locale and why do I have it set like this?
First of all, Europe is a continent not a country. It doesn’t have a country code. It does, however, have a region code. That region code is indeed 150.
Continents have numeric area codes instead including 001 for “World” and 150 for “Europe”.
Be sure to read my article “Are there standard language codes for ‘World English’ and ‘European English’” for a much more detailed explanation. These codes are based in UN M.49, which is the basis for ISO 3166, which is the standard you’re probably thinking of when you say “country codes”.
The problem you’re facing is that you can’t use someone’s language or formatting preferences to determine their physical location. My current device’s locale is en_DK but my physical location is NO. Many devices default to en_US and users all over the world never change the default settings.
Please use CoreLocation to automatically determine the user’s location, or just ask the user to disclose their location.

Can you determine what country your user is in from an iOS app?

I'm trying to determine whether to use imperial or metric units automatically before asking the user to enable location services. I know that you can't get precise data but all I really need is United States or not. Can you determine what store it was downloaded from or use IP address or anything like that?
You could use the device's locale to achieve this...
Obj-C
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
Swift
let locale = NSLocale.currentLocale()
let countryCode = locale.objectForKey(NSLocaleCountryCode)
Country codes are in the format US (United States), FR (France), etc...
Note that the locale is based on the user's device settings and not necessarily the current physical location of the device.
You're probably looking for NSLocale:
let theLocale = NSLocale.autoupdatingCurrentLocale()
print(theLocale.objectForKey(NSLocaleMeasurementSystem))
print(theLocale.objectForKey(NSLocaleUsesMetricSystem))
Look at the NSLocale class reference for more options.
Swift 3
NSLocale.current
NSLocale.Key.countryCode
Yes, annoyingly renamed again, don't you have more important stuff to attend Swift team???

Get currency symbol from currency codes in Objective-C

I'm having trouble getting the currency symbol (e.g. '$') out of currency codes (e.g. 'USD').
I'm currently trying this way:
NSString *code = [NSLocale ISOCurrencyCodes][indexPath.row];
NSLog(#"%#", [[NSLocale currentLocale] displayNameForKey:NSLocaleCurrencySymbol value:code]);
Receiving (null) as result... Looking for the correct way to get the currency symbol from currency codes.
Even though I haven't seen the result yet, I'm pretty sure this will give repeated entries (Euro zone countries for example will be repeated, and will end up with many different '€' symbols). Is there a better approach to this problem?
EDIT: I'm not having problems with NSNumberFormatter, what I need is a list of all currencies supported in iOS for the user to select one (I will handle the formatting of the correct currency with the NSNumberFormatter, not having trouble with that).
Thanks
I can't reproduce your issue. The following works fine:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
for (NSString *code in [NSLocale ISOCurrencyCodes]) {
NSLog(#"%# -> %#", code, [[NSLocale currentLocale] displayNameForKey:NSLocaleCurrencySymbol value:code]);
}
}
return 0;
}
I suspect that your problem is that indexPath.row isn't what you expect it to be.
You shouldn't expect many duplicates (if any). The list of currency codes doesn't list countries; it lists currencies. So there's just one EUR which maps to €. On my system, all but two resolve (EQE and LSM which are discontinued currencies).
Keep in mind that there are 299 different currencies in the list I'm looking at, so your pick list may be much longer than you're thinking.
I don't know if you've figured this out already.
I've had similar issues running on the emulator, but not a device. If you're running on the emulator, see if manually setting the language and region helps. I've noticed this is specific to when your scheme is relying on "System Language" and "System Region". If I manually set the language to "English" and region to "United States" that it can determine the currency symbols again.
Steps to do so:
Click on your scheme -> click "edit scheme" -> make sure you've selected the run option in the side bar -> "Options" -> Application Language and Application Region set to not defaults system.
Let me know if this helps.

Localizing iPhone app for Region

I've got an app that has all content regardless of language displaying content in English. In the products section of the app product content is displayed based on a plist. Products available for purchased are based on location, not all products are available in every market.
In the settings of my simulator I've got my language set to English and my Region Format set to Singapore.
Above my loading of the plist which has been localized, I first do a log to check that I am in fact seeing SG (Singapore) as my region.
NSString *locale = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
NSLog(#"LOCALE: %#", locale);
if([locale isEqualToString:#"SG"]){
NSLog(#"singapore do something?");
productCategory = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:#"Products" ofType:#"plist"]];
}
The current result is showing my log statement logging LOCALE: SG which is expected, however my Singapore specific content is not loading.
I have tried both cleaning the project, and deleting the app from the simulator.
This is how my plist files appear in my project navigator
What am I doing incorrectly that is preventing my localized plist from being displayed?
Localization (the process of loading translated resources from the relevant language folders in your application bundle) is based exclusively on the language setting. So pathForResource only cares about the language setting and ignores the region format setting.
The region format setting affects the conversion between strings and locale-dependent data types (in both directions: parsing input and formatting output). For example if you convert a NSDate to a string for display, depending on the region format setting you might get the month before the day (as in the US) or the opposite (as in the UK).
[NSLocale currentLocale] refers to the region format, so you were simply looking at the wrong thing in your debugging.
There is plenty more info on this here: https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPInternational/BPInternational.html#//apple_ref/doc/uid/10000171i
Edit
See the comments below, this appears to be more complex. It looks like Region does affect localisation when the language is set to a neutral language (e.g. "en" but not "en-US").
I had once the same problem, somehow a non-localized file was found. What worked for me was to use:
[[NSBundle mainBundle] pathForResource:#"Products" ofType:#"plist" inDirectory:nil]
This will always search for all localized files and return the correct one based on the users settings

Resources