Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving a country telephone code for a specific country in iOS

I'm quite new to iOS development and I'm currently building my first app. I'm trying to get a text field to automatically populate the telephone country code for a specific country.

So if for example the user picks "UK" he gets "+44" inserted automatically into that text field.

Currently I'm struggling a way to find how to get the exact country telephone code for the country. I could create an NSDictionary with all of the countries and country telephone code but I thought there might be a better way.

like image 701
Ofir Beigel Avatar asked Dec 17 '22 06:12

Ofir Beigel


2 Answers

If your goal is to get the dialling code of the user's current location then you should use HMDiallingCode.

It uses CoreLocation and reverse geocoding to get current country of the user and then retrieve it's dialling code.

like image 109
Hesham Avatar answered Dec 28 '22 08:12

Hesham


I think, you can only get country code for the current Carrier using CoreTelephony framework:

CTTelephonyNetworkInfo *info = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = info.subscriberCellularProvider;

NSLog(@"country code is: %@", carrier.mobileCountryCode);

If you need a full list of codes for all countries, you need to use some online service for querying it.

like image 42
Denis Avatar answered Dec 28 '22 10:12

Denis