Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user's country calling code in iOS?

Tags:

xcode

iphone

ios5

I am developing an iOS app in which the user enters their mobile number. How do I get their country calling code? For example, if a user is in India, then +91 should be prefixed automatically. Is there an option that adds country codes automatically?

like image 905
user2681789 Avatar asked Sep 14 '13 04:09

user2681789


People also ask

How can I get current country code in iOS?

Go to Product -> Scheme -> Edit Scheme... -> Run -> Options and choose the Application Language and Application Region you want to test.

How do you get a country code?

getCountry(Locale. getDefault()); System. out. println("country = "+locale);

What is my country calling code?

Calling the USA: +1 is the US Country Code.

Why does my phone show country code?

This happens when there is a mismatch between the number associated with the conversation in Messages and the number associated with the person in Contacts.


2 Answers

Import Statement :

#import<CoreTelephony/CTCarrier.h> #import <CoreTelephony/CTTelephonyNetworkInfo.h> 

you can get country code for the current Carrier using CoreTelephony framework:

CTTelephonyNetworkInfo *network_Info = [CTTelephonyNetworkInfo new]; CTCarrier *carrier = network_Info.subscriberCellularProvider;  NSLog(@"country code is: %@", carrier.mobileCountryCode);  //will return the actual country code NSLog(@"ISO country code is: %@", carrier.isoCountryCode); 

Apple Docs

like image 160
iOS_DEV Avatar answered Oct 08 '22 00:10

iOS_DEV


with the use of NSLocale you can get the country name, code etc. Take a look at below code it will help you to do so.

NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale. NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; // get country code, e.g. ES (Spain), FR (France), etc. 

for a countries dialing code you can visit this reference code.

like image 36
D-eptdeveloper Avatar answered Oct 08 '22 01:10

D-eptdeveloper