30

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 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?

idmean
  • 14,540
  • 9
  • 54
  • 83
Maciej Kozieł
  • 959
  • 11
  • 24
  • What's your end goal here? Are you trying to check if something is an iPhone? And how are you calling `canOpenURL:`? – thegrinner Sep 16 '14 at 15:51
  • No, I need to check whether device has this capability to hide phone button if user cannot use it. Or show appropriate message. I'm calling it that way: [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]] – Maciej Kozieł Sep 16 '14 at 15:54
  • 1
    FYI - I asked this on the [Apple Developer Forums](https://devforums.apple.com/message/1039439#1039439) a while ago and didn't get a good answer. – rmaddy Sep 16 '14 at 15:54
  • 2
    FWIW: Here's the behavior I've observed: When I have Continuity setup and iPad can make calls via my local iPhone, tel: links work and complete the call. This can be done either within a web browser (an HTML tel: url) or with openUrl:. If Continuity is not setup, HTML tel: links launch Facetime but then nothing happens. openUrl: called as, say, the result of a button tap, just doesn't do anything at all. I'm also hoping for a way to check that the user can/can't perform this action based on their Continuity status. – Brian Colavito Apr 15 '15 at 21:17
  • Hi, @MaciejKozieł , have you found any solution? I have the same issue and this iOS behavior is still in the Apple's latest iOS versions. – Alexander Tkachenko May 15 '15 at 08:37
  • If you get MCC/MNC you should be able to place a call (there is a valid SIM present and device is inside a cellular service range)... https://developer.apple.com/library/prerelease/ios/documentation/NetworkingInternet/Reference/CTCarrier/index.html#//apple_ref/occ/cl/CTCarrier – Rok Jarc May 19 '15 at 20:47

2 Answers2

16

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. 
    }
}
JoelC
  • 3,664
  • 9
  • 33
  • 38
Milan Shah
  • 205
  • 1
  • 3
  • 15
-4

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
}
Wapiti
  • 80
  • 1
  • 9
  • 3
    This is not correct solution. Phone calls are also possible on iPad. – Maciej Kozieł Jan 10 '15 at 23:39
  • @MaciejKozieł, I don't think that's what he was asking. Sounded to me like he was trying to discriminate against the iPad. He mentioned that - (BOOL)canOpenURL:(NSURL *)url was returning YES on his iPad, but he didn't want that even if it could call over wifi. – Wapiti Jan 11 '15 at 05:21
  • First, it is my question. Second, if question is not clear to you, please read comments. – Maciej Kozieł Jan 11 '15 at 14:03
  • @MaciejKozieł, Sorry, didn't realize it was your question. In several of my apps I need to show/hide a phone button based on the ability to make a phone call. My users complained when the phone button was available on an iPad. Even after reading your comments it sounded like you were dissatisfied with - (BOOL)canOpenURL: returning YES on an iPad. So I offered what made my users happy. Good luck. – Wapiti Jan 12 '15 at 05:20
  • 1
    Problem is, actually there seems to be no way to determine whether device can make phone call. I don't want to use hacks on that. Furthermore, I'd like to present phone button on iPad, when phone calls are possible for that user. Thanks. – Maciej Kozieł Jan 12 '15 at 13:06
  • @MaciejKozieł Can you please describe the hardware where an iPad is able to make phone calls as per your requirements? – RyanR May 19 '15 at 20:42