Determine country code for iOS Region "Europe" - ios

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.

Related

how do i get my iPhone device country code without using GPS or location service?

I need help...
I am new to iPhone app development and working on a registration page.
Scenario:->
Location services is off.
Two text fields, one for phone country code and other for phone number.
When registration page pops up, country code should be automatically filled with the country code of the device.
Problem:-> I am having difficulty in fetching the country code from device information.
Also, what info may I use for the same... IMEI, MDN, Locale, etc...
Any response is appreciated..!
You can get this information from iOS' CoreTelephony framework:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
...
#property (nonatomic, strong) CTTelephonyNetworkInfo* networkInfo;
...
self.networkInfo = [[CTTelephonyNetworkInfo alloc] init];
NSString* country = [self.networkInfo subscriberCellularProvider].isoCountryCode;
I quickly copy-pasted this from one of my apps. I trust you can convert this to Swift if you need to.
subscriberCellularProvider gives access to the CTCarrier object, which has more fields than just the country code: MCC, MNC, carrier name, and a flag if VoIP is allowed.
iOS does not give access to IMEI.
The In App Purchase APIs can tell you on which iTunes Store the user is; an ISO country code again. You probably need to have IAP activated for your app, so when you don't sell anything, this may not be allowed/an option.
The current locale can indeed also give you a country code:
let locale = NSLocale.currentLocale()
let country = currentLocale.objectForKey(NSLocaleCountryCode) as? String

How do I create an NSLocale from a Country Code?

I'm trying to create an NSLocale based on the location of an iOS device. From the device, I can get a CLLocation. From the CLLocation, I can get [CLPlacemark]. From one of the [CLPlacemark], I can get the ISOcountryCode, but I don't know how to use this to create an NSLocale.
When I do this my country code is "GB". The availableLocaleIdentifiers() that contain "GB" are "kw_GB", "gd_GB", "en_GB" and "cy_GB". I think the identifier I want, in this case, is en_GB, but how to I programatically choose this?

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???

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

NSLocaleUsesMetricSystem always YES on iPad

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.

Resources