Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible way to detect sim card detection in ios?

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?

like image 399
vinoth kannan Avatar asked Dec 25 '13 05:12

vinoth kannan


3 Answers

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.

like image 54
Aneeq Anwar Avatar answered Nov 13 '22 22:11

Aneeq Anwar


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");
        });
};
like image 21
Balaji Sabdhar M Avatar answered Nov 13 '22 23:11

Balaji Sabdhar M


Implemented in Swift as a read-only computed property:

import CoreTelephony

var availableSIM: Bool {
    return CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode != nil
}
like image 27
Leo Dabus Avatar answered Nov 13 '22 21:11

Leo Dabus