5

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
    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

3 Answers3

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,274
  • 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
  • 3,988
  • 6
  • 46
  • 69