How to check if device can make a phone call (iOS 8)?

前端 未结 2 1888
感动是毒
感动是毒 2020-12-05 00:20

On iOS <8 you could use function - (BOOL)canOpenURL:(NSURL *)url.

On iOS 8 this function returns YES, even on iPad. I guess it\'s connec

相关标签:
2条回答
  • 2020-12-05 01:02

    Ok, so I just encountered the same problem. Seems like iPad and iPod return YES value for canOpenURL method. Please see my answer below since this worked for me. I had a custom collection view cell and that is why had this code in my awakeFromNib file. However, you should write this code in ViewDidLoad of that perticular viewController.

    Make sure to include "CoreTelephony.Framework" in your project.

    Include below files in the view controller:

     #import <CoreTelephony/CTTelephonyNetworkInfo.h>
     #import <CoreTelephony/CTCarrier.h>
    
        - (void)awakeFromNib {
        // Initialization code
    
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]) {
            // Check if iOS Device supports phone calls
            CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
            CTCarrier *carrier = [netInfo subscriberCellularProvider];
            NSString *mnc = [carrier mobileNetworkCode];
            // User will get an alert error when they will try to make a phone call in airplane mode.
            if (([mnc length] == 0)) {
                // Device cannot place a call at this time.  SIM might be removed.
            } else {
                // iOS Device is capable for making calls
            }
        } else {
            // iOS Device is not capable for making calls
        }
    
    
    
        if ( ! [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"sms:"]]) {
           // iOS Device is not capable to send SMS messages. 
        }
    }
    
    0 讨论(0)
  • 2020-12-05 01:13

    You could just see if it's an iPhone. And possibly use this in conjunction with - (BOOL)canOpenURL:(NSURL *)url. That way you avoiding devices that obviously can't make a cellular phone call.

    if ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"] ) {
         // Make Phone Call
    }
    
    0 讨论(0)
提交回复
热议问题