如何检查设备是否可以拨打电话(iOS 8)? [英] How to check if device can make a phone call (iOS 8)?

查看:127
本文介绍了如何检查设备是否可以拨打电话(iOS 8)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS< 8上你可以使用函数 - (BOOL)canOpenURL:(NSURL *)url

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

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

On iOS 8 this function returns YES, even on iPad. I guess it's connected with calling over wi-fi (or another new functionality), but my iPad cannot make phone calls. Anyone know better way to detect that capability?

推荐答案

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

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.

确保在项目中包含CoreTelephony.Framework

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

这篇关于如何检查设备是否可以拨打电话(iOS 8)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆