【问题标题】:How to get the country code from simCard programatically in ios objective c如何在ios目标c中以编程方式从sim卡中获取国家代码
【发布时间】:2021-07-07 06:10:11
【问题描述】:

实际上,我正在尝试以编程方式从我手机中的 SimCard 获取国家代码。我们如何以编程方式从 Simcard 获取国家代码。或者是否有任何 pod 可以帮助我获取国家/地区代码详细信息。

提前致谢

【问题讨论】:

标签: ios objective-c


【解决方案1】:

1. 来自 SimCard

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

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

注意:此代码在以下情况下不起作用:

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

2. 来自 CurrentLocale

NSLocale *currentLocale = [NSLocale currentLocale];  // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
NSLog(@"countryCode: %@", countryCode);

【讨论】:

    【解决方案2】:

    你可以导入:

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

    并使用此代码:

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

    希望对你有帮助。

    【讨论】:

    • 如果您只是创建一个 CTCarrier 对象,它将不会填写任何信息。您必须使用:CTCarrier *carrier = [[CTTelephonyNetworkInfo new]subscriberCellularProvider];就像在另一个答案中一样。请编辑或删除此答案。
    【解决方案3】:

    对于那些急需这个的人。

    import CoreTelephony
    
    let networkProviders = CTTelephonyNetworkInfo().serviceSubscriberCellularProviders
    let countryCode = networkProviders?.first?.value.isoCountryCode
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 2016-06-11
      • 2017-12-31
      相关资源
      最近更新 更多