美文网首页
iOS获取SIM卡信息(判断设备是否安装SIM卡)

iOS获取SIM卡信息(判断设备是否安装SIM卡)

作者: OwenWong | 来源:发表于2020-03-05 17:47 被阅读0次

引用:

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

一、判断设备是否安装SIM卡:

/// 方法一:获取SIM卡所属国家码(旧方法)
+(BOOL)isSIMInstalled
{
// 双卡获取会有异常
    CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [networkInfo subscriberCellularProvider];
    if (!carrier.isoCountryCode) {
        NSLog(@"No sim present Or No cellular coverage or phone is on airplane mode.");
        return NO;
    }
    return YES;
}
///方法二:获取手机中sim卡数量(推荐)
- (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;
          }
     }
}

二、获取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
    }
}
运行结果: sim卡信息.png

附:iOS获取硬件信息

相关文章

网友评论

      本文标题:iOS获取SIM卡信息(判断设备是否安装SIM卡)

      本文链接:https://www.haomeiwen.com/subject/uibqrhtx.html