iPhone - 检测SIM卡可用性

时间:2012-05-07 20:46:53

标签: iphone ios core-telephony

我正在使用本主题中的答案。 iPhone - how to determine carrier of the device (AT&T, Verizon, etc?)getting operator details in iphone相同。虽然使用SIM卡时工作正常,但如果没有SIM卡,则返回的运营商名称是旧的运营商名称。它没有检测到SIM被移除。

我知道这与Apple文档相矛盾,如果没有载体,CTCarrier对象应为零。但在我的应用程序中,我记录了运营商信息,它为我提供了最新的运营商名称,但没有安装SIM卡。

6 个答案:

答案 0 :(得分:11)

根据[CTCarrier carrierName]的{​​{3}}:

  

如果您为运营商配置设备,然后移除SIM卡,   此属性保留了承运人的名称。

据我所知,您无法检测SIM卡是否已安装。您只能使用documentation确定WWAN连接是否可用。

答案 1 :(得分:8)

@import CoreTelephony;

-(BOOL)hasCellularCoverage
{
    CTTelephonyNetworkInfo *networkInfo = [CTTelephonyNetworkInfo new];
    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;
}

答案 2 :(得分:6)

CTCarrier对象有5个属性:

allowsVOIP
carrierName
isoCountryCode
mobileCountryCode
mobileNetworkCode

我已经对CTCarrier进行了一些测试,我得出的结论是,对于iOS 7 仅删除了运营商名称和allowVOIP 。 isoCountryCode,mobileCountryCode和mobileNetworkCode将针对iOS 7重置。这就是您可以检测SIM是否存在的方法。

对于iOS 6,所有值都会保留。

我使用运行iOS 7的iPhone 4S和iPhone 5进行了测试。

答案 3 :(得分:0)

Swift版本:

TIMESTAMP on update CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        okHttpClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        okHttpClient.setWriteTimeout(WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);

        List<Protocol> protocols = new ArrayList<>();
        protocols.add(Protocol.HTTP_1_1);
        okHttpClient.setProtocols(protocols);

答案 4 :(得分:0)

Swift 5.4 答案

接受的答案已过时或不正确。

protocol SimCardServiceProtocol {
    var isAvailableSIM: Bool { get }
}

final class SimCardService: SimCardServiceProtocol {

    var isAvailableSIM: Bool {
        return CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.first?.value.mobileNetworkCode != nil
    }
}

答案 5 :(得分:0)

希望能帮到你:

    if #available(iOS 12.0, *) {
        return CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.first?.value.mobileNetworkCode != nil
    } else {
        if let _ = CTTelephonyNetworkInfo().subscriberCellularProvider?.isoCountryCode {
            return true
        } else {
            return false
        }
    }
相关问题