引用:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
一、判斷設(shè)備是否安裝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;
}
///方法二:獲取手機(jī)中sim卡數(shù)量(推薦)
- (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 = @"移動運(yùn)營商";
} else if ([code isEqualToString:@"01"] || [code isEqualToString:@"06"] || [code isEqualToString:@"09"]) {
operatorName = @"聯(lián)通運(yùn)營商";
} else if ([code isEqualToString:@"03"] || [code isEqualToString:@"05"] || [code isEqualToString:@"11"]) {
operatorName = @"電信運(yùn)營商";
} else if ([code isEqualToString:@"20"]) {
operatorName = @"鐵通運(yùn)營商";
}
NSLog(@"%@",operatorName);
}];
} else {
// Fallback on earlier versions
}
}
運(yùn)行結(jié)果:
附:iOS獲取硬件信息