简体   繁体   English

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

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

On iOS <8 you could use function - (BOOL)canOpenURL:(NSURL *)url . 在iOS <8上你可以使用函数- (BOOL)canOpenURL:(NSURL *)url

On iOS 8 this function returns YES , even on iPad. 在iOS 8上,即使在iPad上,此功能也会返回YES I guess it's connected with calling over wi-fi (or another new functionality), but my iPad cannot make phone calls. 我猜它与通过wi-fi(或其他新功能)通话有关,但我的iPad无法拨打电话。 Anyone know better way to detect that capability? 任何人都知道检测该功能的更好方法吗?

Ok, so I just encountered the same problem. 好的,所以我刚遇到同样的问题。 Seems like iPad and iPod return YES value for canOpenURL method. 似乎iPad和iPod为canOpenURL方法返回YES值。 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. 我有一个自定义集合视图单元格,这就是为什么这个代码在我的awakeFromNib文件中。 However, you should write this code in ViewDidLoad of that perticular viewController. 但是,您应该在该特定viewController的ViewDidLoad中编写此代码。

Make sure to include "CoreTelephony.Framework" in your project. 确保在项目中包含“CoreTelephony.Framework”

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

You could just see if it's an iPhone. 你可以看看它是不是iPhone。 And possibly use this in conjunction with - (BOOL)canOpenURL:(NSURL *)url . 并且可能与- (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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM