iOS how to find country code of the user's phone number?
Asked Answered
G

1

9

I have an app that needs to figure out the country code of the phone number the user has. My understanding is that I can't just get the phone number of the user, but, is there a way for example if I have a US phone number to get the country code +1?

I have found multiple answers that claim that using core telephony you can get the country code "+1" but I tried a few and none of them work.

I tried this:

CTTelephonyNetworkInfo *network_Info = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = network_Info.subscriberCellularProvider;
NSString *mnc = [carrier mobileNetworkCode];
NSString *mcc = [carrier mobileCountryCode];    
NSLog(@"country code is: %@, mnc: %@, mcc: %@", carrier.mobileCountryCode, mnc, mcc);

which prints:

"country code is: 310, mnc: 410, mcc: 310"

So, for a US number I would like to get +1.

Is there a way to do this?

EDIT: With further search I found this:

How to map MCC + MNC to users phone country code in iOS objective-C without using Location info?

It might do the trick, but I was looking for a more official way to do this.

Gnash answered 2/8, 2014 at 13:55 Comment(2)
There is no API for this. Find a database, include it in app and look up this information there.Essay
Easier said than done: en.wikipedia.org/wiki/List_of_country_calling_codesSauterne
J
10

You can get the country code with:

NSString *cc = [carrier isoCountryCode];

Which will give you US for a USA based device. You can map from country code to country phone code via the JSON data available on this.

Jarodjarosite answered 12/10, 2014 at 23:54 Comment(1)
This works. NOTE: it won't work on the simulator since it does not run on a specific "carrier" thus carrier will be nil and won't be able to return isoCountryCode. Nice API!Gnash

© 2022 - 2024 — McMap. All rights reserved.