How do I create an NSLocale from a Country Code? - ios

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?

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

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

How to get the user "Name" using Swift?

I'm trying to create an app that when the user open the first view it will provide with a nice greeting.
For example depending on the time of the day it would say "Good Morning/Afternoon/Night - User Name".
How do I get the name that the User has on his iPhone? I need the name that is on Settings > General > About _ NAME?
UIDevice.current.name gives back device name, so you should parse the string trying to distinguish between device type (iPhone, iPad and so on...) and the name.
But is bit tricky: suppose You have "My iPhone" , "Mary's iPhone" or "IPhone of John" or similar.
Soem people try to use regular expressions, but result is not so good, specially for non-english names.
see at:
http://stackoverflow.com/questions/8261961/better-way-to-get-the-users-name-from-device
Swift 2
UIDevice.currentDevice().name
Swift 3
UIDevice.current.name
let benutzerGeraeteName = UIDevice.current.name
print("Hallo, \(benutzerGeraeteName)")

How can I get details about the device data provider (like Verizon/AT&T) of an iphone programmatically?

I am trying to create an ios application and I want to segment the users based on the data providers they are using, such as Verizon and AT&T. Is it possible to get this information programmatically from the ios application.
You should check the CTCarrier.
Just import CoreTelephony into your Swift file.
Then you can use the carrierName property to get the name of your carrier.
// Setup the Network Info and create a CTCarrier object
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.subscriberCellularProvider
// Get carrier name
let carrierName = carrier.carrierName
Now that you can have multiple SIM cards, subscriberCellularProvider is deprecated in favor of serviceSubscriberCellularProviders. You can get an array of the providers with this:
let carriers = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.values
In my case, I was checking to see if the user has an American phone number so they can text support instead of email. You can do that with this:
carriers.contains { $0.isoCountryCode?.lowercased() == "us" }
On my phone, I only have one SIM card, but this array returns two values and one has all nil properties so be sure to handle that if you are inspecting them.
You will want to use the CTCarrier carrierName in the CoreTelephony framework: https://developer.apple.com/library/prerelease/ios/documentation/NetworkingInternet/Reference/CTCarrier/index.html#//apple_ref/occ/instp/CTCarrier/carrierName

Resources