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
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
The use of CTCarrier
, a deprecated API, returns static values for apps that are built with the iOS 16.4 SDK or later. (76283818)
iOS 16.4 Release notes
As of above change, Apple has not provided a replacement class to read country from SIM code. Fingers crossed on if Apple may delay this change.
Unfortunately, we may have to stick to below code to read a country code, which is in deed not a solution.
if let code = Locale.current.regionCode {
// country code read from the device instead of SIM
}