6

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

Santhosh
  • 525
  • 5
  • 13
  • 1
    try these links http://stackoverflow.com/questions/9095023/ios-country-code and this http://stackoverflow.com/questions/13584981/identifying-the-country-code-using-mobile-carrier-in-iphone-programatically – caldera.sac Aug 11 '16 at 10:02

4 Answers4

12

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);
Bin0li
  • 168
  • 16
4

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.

Alanc Liu
  • 1,294
  • 10
  • 15
  • 1
    If you just create a CTCarrier object it will not have any of the information filled in. You must use: CTCarrier *carrier = [[CTTelephonyNetworkInfo new] subscriberCellularProvider]; as in the other answer. Please edit or remove this answer. – xpereta Jun 12 '18 at 06:37
1

For those who need this in swift.

import CoreTelephony

let networkProviders = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders
let countryCode = networkProviders?.first?.value.isoCountryCode
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
0

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.

Class Documentation Ref

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
 }
Tharindu Madushanka
  • 3,241
  • 7
  • 31
  • 33