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
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);
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.
For those who need this in swift.
import CoreTelephony
let networkProviders = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders
let countryCode = networkProviders?.first?.value.isoCountryCode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With