iOS获取双Sim卡信息

最近遇到一个需求,需获取XR以及XS Max上的双Sim卡信息,网上没有找到合适的方法,从iOS12内部文档中发现了此方法,分享给大家(PS:只能获取到双Sim的运营商,手机号码暂时没找到方法获取到):

  • 需引用 :
#import 
#import 
  • 方法如下:
/**
获取sim卡信息
*/
- (void)getOperatorInfomation {
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    //iOS12以上可使用
    if (@available(iOS 12.0, *)) {
        NSDictionary *carrierDic = [info serviceSubscriberCellularProviders];
        __block NSString *operatorName = [NSString string];
        [carrierDic.allValues enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CTCarrier *carrier = obj;
            //NSLog(@"carrier = %@", carrier);
            NSString *code = [carrier mobileNetworkCode];
            if (code == nil) {
                operatorName = @"不能识别";
            }
            if ([code isEqualToString:@"00"] || [code isEqualToString:@"02"] || [code isEqualToString:@"04"] || [code isEqualToString:@"07"] || [code isEqualToString:@"08"]) {
                operatorName = @"移动运营商";
            } else if ([code isEqualToString:@"01"] || [code isEqualToString:@"06"] || [code isEqualToString:@"09"]) {
                operatorName = @"联通运营商";
            } else if ([code isEqualToString:@"03"] || [code isEqualToString:@"05"] || [code isEqualToString:@"11"]) {
                operatorName = @"电信运营商";
            } else if ([code isEqualToString:@"20"]) {
                operatorName = @"铁通运营商";
            }
            NSLog(@"%@",operatorName);
        }];
    } else {
        // Fallback on earlier versions
    }
}
  • 运行展示:


    iOS获取双Sim卡信息_第1张图片
    sim卡信息.png
  • 获取iPhone中当前sim卡数量方法,跟上边大同小异:

/**
判断手机中sim卡数量

@return 返回值 int 个数
*/
- (int)SimCardNumInPhone {
     CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
     if (@available(iOS 12.0, *)) {
          NSDictionary *ctDict = networkInfo.serviceSubscriberCellularProviders;
          if ([ctDict allKeys].count > 1) {
               NSArray *keys = [ctDict allKeys];
               CTCarrier *carrier1 = [ctDict objectForKey:[keys firstObject]];
               CTCarrier *carrier2 = [ctDict objectForKey:[keys lastObject]];
               if (carrier1.mobileCountryCode.length && carrier2.mobileCountryCode.length) {
                    return 2;
               }else if (!carrier1.mobileCountryCode.length && !carrier2.mobileCountryCode.length) {
                    return 0;
               }else {
                    return 1;
               }
          }else if ([ctDict allKeys].count == 1) {
               NSArray *keys = [ctDict allKeys];
               CTCarrier *carrier1 = [ctDict objectForKey:[keys firstObject]];
               if (carrier1.mobileCountryCode.length) {
                    return 1;
               }else {
                    return 0;
               }
          }else {
               return 0;
          }
     }else {
          CTCarrier *carrier = [networkInfo subscriberCellularProvider];
          NSString *carrier_name = carrier.mobileCountryCode;
          if (carrier_name.length) {
               return 1;
          }else {
               return 0;
          }
     }
}
  • 以开发角度看系统权限,下图取自https://juejin.im/post/5bf2a86f6fb9a049c84f0ed2
    权限列表.png
  • 希望对大家有帮助,方便的话点个喜欢 .谢谢.

你可能感兴趣的:(iOS获取双Sim卡信息)