Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the country code from simCard programatically in ios objective c

Actually I am trying to get the country code programatically from SimCard which is in my phone . How can we get the country code from Simcard programatically . or is there any pod that could help me get the country code details .

Thanks in advance

like image 874
Chintu the cool kid Avatar asked Aug 11 '16 09:08

Chintu the cool kid


3 Answers

1. From SimCard

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

CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider];
NSString *countryCode = carrier.isoCountryCode;
NSLog(@"countryCode: %@", countryCode);

Note : This code does not work in following conditions:

1.Airplane mode.
2.No SIM card in the device.
3.Device is outside the cellular service range.

2. From CurrentLocale

NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
NSLog(@"countryCode: %@", countryCode);
like image 173
Chirag D jinjuwadiya Avatar answered Nov 15 '22 01:11

Chirag D jinjuwadiya


You can import:

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

And using this code:

CTCarrier* tmpCTC = [[CTCarrier alloc] init];
NSString* mcc = [tmpCTC mobileCountryCode];
NSLog([NSString stringWithFormat:@"mcc = %@",mcc]);

Hope it can help you.

like image 27
Alanc Liu Avatar answered Nov 15 '22 01:11

Alanc Liu


For those who need this in swift.

import CoreTelephony

let networkProviders = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders
let countryCode = networkProviders?.first?.value.isoCountryCode
like image 1
Declan McKenna Avatar answered Nov 15 '22 01:11

Declan McKenna