Possible way to detect sim card detection in ios?

前端 未结 6 1866
野趣味
野趣味 2020-12-31 20:33

I have a iphone app that has the capability to send messages. I want to alert user when sim card is not available in iphone. So i tried below three function to check sim car

相关标签:
6条回答
  • 2020-12-31 20:45

    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

    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");
            });
    };
    
    0 讨论(0)
  • 2020-12-31 20:49

    Complementing the Anneq Anwar answer, here the swift version:

    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

    0 讨论(0)
  • 2020-12-31 20:52

    Implemented in Swift as a read-only computed property:

    import CoreTelephony
    
    var availableSIM: Bool {
        return CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode != nil
    }
    
    0 讨论(0)
  • 2020-12-31 21:01

    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
    }
    
    0 讨论(0)
  • 2020-12-31 21:03

    Complementing to Anneq and Felipe answers:

    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
    }
    
    0 讨论(0)
  • 2020-12-31 21:07

    You can check it by CTCarrier class.

    enter image description here

    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.

    0 讨论(0)
提交回复
热议问题