如何检查设备是否可以拨打电话(iOS 8)?

Mac*_*ieł 31 objective-c ipad ios ios8

在iOS <8,您可以使用功能- (BOOL)canOpenURL:(NSURL *)url.

在iOS 8上,此功能YES即使在iPad上也会返回.我猜它与通过wi-fi(或其他新功能)通话有关,但我的iPad无法拨打电话.任何人都知道检测该功能的更好方法吗?

Mil*_*hah 16

好的,所以我刚遇到同样的问题.似乎iPad和iPod为canOpenURL方法返回YES值.请看下面的答案,因为这对我有用.我有一个自定义集合视图单元格,这就是为什么这个代码在我的awakeFromNib文件中.但是,您应该在该特定viewController的ViewDidLoad中编写此代码.

确保在项目中包含"CoreTelephony.Framework".

在视图控制器中包含以下文件:

 #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. 
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 此外,我怀疑iPad中的数据SIM会出现误报,所以我建议添加一个UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad的检查 (3认同)

Wap*_*iti -4

你可以看看它是否是iPhone。并且可能将其与 结合使用- (BOOL)canOpenURL:(NSURL *)url。这样您就可以避开那些显然无法拨打手机的设备。

if ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"] ) {
     // Make Phone Call
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是正确的解决方案。iPad 上也可以拨打电话。 (3认同)