简体   繁体   English

在ios中检测sim卡检测的可能方法?

[英]Possible way to detect sim card detection in ios?

I have a iphone app that has the capability to send messages.我有一个能够发送消息的 iphone 应用程序。 I want to alert user when sim card is not available in iphone.我想在 iphone 中没有 sim 卡时提醒用户。 So i tried below three function to check sim card availabilty所以我尝试了以下三个功能来检查 sim 卡的可用性

Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
if([messageClass canSendText]){
    // Sim available
    NSLog(@"Sim available");
}
else{
    //Sim not available
    NSLog(@"Sim not available");
}

if([MFMessageComposeViewController canSendText]){
    // Sim available
    NSLog(@"Sim available");
}
else{
    //Sim not available
    NSLog(@"Sim not available");
}

if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:123456"]])
    {
        NSLog(@"Sim available");
    }
    else
    {
        NSLog(@"Sim not available");
    }
}

I have checked my iphone without sim, it always return @"Sim available".我检查了没有 sim 卡的 iphone,它总是返回 @"Sim available"。 But when i open default "Messages" app and try to send sms it says alert "No SIM Card Installed"... How this message app can detect sim card availabilty?但是,当我打开默认的“消息”应用程序并尝试发送短信时,它会提示“未安装 SIM 卡”...此消息应用程序如何检测 sim 卡的可用性?

You can check it by CTCarrier class. 您可以通过CTCarrier课程进行检查。

在此输入图像描述

BOOL isSimCardAvailable = YES;

CTTelephonyNetworkInfo* info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier* carrier = info.subscriberCellularProvider;

if(carrier.mobileNetworkCode == nil || [carrier.mobileNetworkCode isEqualToString:@""])
{
    isSimCardAvailable = NO;
}

You need to add CoreTelephony framework for using CTTelephonyNetworkInfo and CTCarrier. 您需要添加CoreTelephony框架以使用CTTelephonyNetworkInfo和CTCarrier。

By the below code you can get the sim card details like carriername,mobilecountrycode,isocountrycode,mobilenetworkcode.In the ios 6 all are retained.So if your sim card is removed also it will retain the old details.So there this idea wont be useful but in ios 7 only carriername is retained and remaining are changed so the below code can be used 通过以下代码,您可以获得sim卡详细信息,如carriername,mobilecountrycode,isocountrycode,mobilenetworkcode。在ios 6中所有都被保留。所以如果你的SIM卡被删除也会保留旧的细节。所以这个想法不会有用但是在ios 7中只保留了运营商名称并保留了其余名称,因此可以使用以下代码

CTTelephonyNetworkInfo* info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier* carrier = info.subscriberCellularProvider;
NSString *mobileCountryCode = carrier.mobileCountryCode;
NSString *carrierName = carrier.carrierName;
NSString *isoCountryCode = carrier.isoCountryCode;
NSString *mobileNetworkCode = carrier.mobileNetworkCode

// Try this to track CTCarrier changes 
info.subscriberCellularProviderDidUpdateNotifier = ^(CTCarrier* inCTCarrier) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"User did change SIM");
        });
};

Implemented in Swift as a read-only computed property: 在Swift中实现为只读计算属性:

import CoreTelephony

var availableSIM: Bool {
    return CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode != nil
}

Complementing to Anneq and Felipe answers: 补充Anneq和Felipe的答案:

Swift 2.3: Swift 2.3:

import CoreTelephony
func isSimAvailable()  -> Bool {
    let info = CTTelephonyNetworkInfo()
    let carr = info.subscriberCellularProvider
    guard let carrier = carr else {
        return false
    }
    guard let carrierCode = carrier.mobileNetworkCode else {
        return false
    }
    guard carrierCode != "" else {
        return false
    }
    return true
}

Complementing the Anneq Anwar answer, here the swift version: 补充Anneq Anwar的答案,这里是快速版本:

import CoreTelephony
func isSimAvailable()  -> Bool {
    var isSimCardAvailable = true
    var info = CTTelephonyNetworkInfo()
    var carrier = info.subscriberCellularProvider
    if carrier != nil && carrier.mobileNetworkCode == nil || carrier.mobileNetworkCode.isEqual("") {
        isSimCardAvailable = false
    }
    return isSimCardAvailable
}

fix: The carrier can be nil in some devices 修复:某些设备中的载波可能为零

Swift 4+ Swift 4+

var isSimCardAvailable: Bool {

    let info = CTTelephonyNetworkInfo()
    if let carrier = info.subscriberCellularProvider {
        if let code = carrier.mobileNetworkCode {
            if !code.isEmpty {
                return true
            }
        }
    }
    return false
}

Swift 5+ solution that covers regular SIM and eSIM涵盖普通 SIM 和 eSIM 的 Swift 5+ 解决方案

    func hasCellularCoverage() -> Bool {
        let networkInfo = CTTelephonyNetworkInfo()

        guard let serviceSubscriberCellularProviders = networkInfo.serviceSubscriberCellularProviders else { return false }
        let carriers = serviceSubscriberCellularProviders.values

        let validCarriers = carriers.compactMap() {
            $0.isoCountryCode
        }

        return !validCarriers.isEmpty ? true : false
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM