Possible way to detect sim card detection in ios?

Viewed 11656

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 card availabilty

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". 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?

7 Answers

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

    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
    }
Related