Detecting whether or not device support phone calls?
Asked Answered
I

9

49

Is the below code reliable to be used to determine whether a device can support phone calls or not? My concern is if apple changes the iphone string to anything else let's say they decide to have "iphone 3g", "iphone 4" etc.

[[UIDevice currentDevice].model isEqualToString:@"iPhone"]
Incult answered 23/2, 2011 at 18:0 Comment(0)
F
100

The iPhone supports the tel:// URI scheme. So you could use:

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]];

canOpenURL: explicitly checks whether there's an application capable of opening that URL scheme, not that the URL is correct. So it doesn't matter that no phone number is specified. The method returns a BOOL, so check that for YES or NO.

That should literally answer whether there's any application present capable of making a telephone call. So it should be okay against any future changes in device segmentation.

Filler answered 23/2, 2011 at 18:9 Comment(12)
I want to note something people may overlook with this method. If the device is in Airplane mode, or the SIM card is removed, or the SIM cards service is deactivated, then "technically" the device supports phone calls, but it doesn't mean it "can" place phone calls at the moment. As rare as it might sound, people use old iPhones without service as iPod Touches. Just something to think about depending on what your trying to accomplish.Rilda
Completely agree with AlBeebe, for my app the key is whether the phone can make a call RIGHT NOW not in general. I think AlBeebe's answer below is the correct one.Blear
@Blear it got a plus vote from me at the time but I now think it's the wrong answer since FaceTime will route voice calls by IP from a tel:// link without a carrier connection, and we don't yet know how VoIP is going to factor into things if and when Apple implements it.Filler
I spoke too soon. In airplane mode I get a carrier code of 410, which is the same carrier code when not in airplane mode. Apple says: If you configure a device for a carrier and then remove the SIM card, this property retains the name of the carrier. However, Apple also says for isoCountryCode: The value for this property is nil if any of the following apply: The device is in Airplane mode, there is no SIM card in the device, The device is outside of cellular service range. I will test this and add an answer if it works.Blear
isoCountryCode is not nil for me in Airplane Mode. This directly contradicts Apple's documentation. :-(. Still looking for a solution.Blear
Note this does not work in IOS8 betas. But likely to be a bug in the betas. If not then the CTTelephonyNetworkInfo checks will required.Blackmarketeer
Does not work also o iOS8GM, so looks like not a bug in betas.Pamela
This returns TRUE on my iPad2 running iOS 8.1. However, it returns FALSE (correct behaviour) on my iPad2 8.1 simulator. So unfortunately it doesn't work, where it counts...Genteel
@Alex at a grand guess, this is because, per apple.com/ios/whats-new/continuity "Now you can make and receive phone calls on [your iPad and Mac] as long as your iPhone running iOS 8 is on the same Wi-Fi network." Did you have an iPhone on the same Wi-Fi network? It's possible the iPad would say 'YES' regardless of course, depending on how dynamic Apple's own apps are permitted to be about supported URL schemes.Filler
see this SO thread for iOS 8 discussion.. hopefully an answer will come up that's better in the futurePiebald
To reiterate: iPads will return YES to this check in iOS 8 and up.Foiled
... but that's possible because since iOS 8 iPads can make phone calls? support.apple.com/en-us/HT204681 — "With Continuity, you can make and receive cellular phone calls from your iPad".Filler
R
65

Simply checking if a device "supports" phone calls might not be the best way to go about things depending on what your trying to accomplish. Believe it or not, some people use old iPhones without service as if they were an iPod Touch. Sometimes people don't have SIM cards installed in their iPhones. In my app I wanted to dial a phone number if the users device was able to, otherwise I wanted to display the phone number and prompt the user to grab a phone and dial it. Here is a solution I came up with that has worked so far. Feel free to comment and improve it.

// You must add the CoreTelephony.framework
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

-(bool)canDevicePlaceAPhoneCall {
    /*

     Returns YES if the device can place a phone call

     */

    // Check if the device can place a phone call
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel://"]]) {
        // Device supports phone calls, lets confirm it can place one right now
        CTTelephonyNetworkInfo *netInfo = [[[CTTelephonyNetworkInfo alloc] init] autorelease];
        CTCarrier *carrier = [netInfo subscriberCellularProvider];
        NSString *mnc = [carrier mobileNetworkCode]; 
        if (([mnc length] == 0) || ([mnc isEqualToString:@"65535"])) {
            // Device cannot place a call at this time.  SIM might be removed.
            return NO;
        } else {
            // Device can place a phone call
            return YES;
        }
    } else {
        // Device does not support phone calls
        return  NO;
    }
}

You'll notice I check if the mobileNetworkCode is 65535. In my testing, it appears that when you remove the SIM card, then the mobileNetworkCode is set to 65535. Not 100% sure why that is.

Rilda answered 21/7, 2012 at 19:45 Comment(8)
This didn't work for me on iOS 8.1.3, mnc was always equal to "02" whether in airplane mode or not. But I found a solution which works on iOS 7 and later, see my answer below.Emetine
I know this is an old topic, but I just wanted to point it out that 65535 MNC is an absolutely valid MNC in South Africa. So depending on your market you need to be careful with this approach.Hedvah
I think you're incorrect @GaborFuredi because in South Africa the MCC is 655 (mobile country code) and 35 would be an MNC (mobile network code) . Two distinct things. In my example i'm checking if an MNC is 65536, and according to en.wikipedia.org/wiki/Mobile_country_code theres no MNC greater then 1,000Rilda
This method is great, but still not perfect. I'm testing something on a 3G iPad which will accept the tel: url and will return with a valid mmc. It will handle the url just fine when you click on the relevant button in the app, but upon selecting the call option in the resulting pop up, nothing happens. I've actually combined your detection with a device name check, so all iPads are excluded. It might not 100% future proof, but it's given the required function.Autarch
Following on @martin-cassidy, data SIMs in iPads will give a false positive, so I suggest adding a check for UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPadFoiled
Thankyou! I've been round the houses looking for this... canOpenUrl is definitely not enoughLiftoff
@GaborFuredi @albeebe Note that the MCC is a 10bit value[1], which someone clearly crammed into a 16bit signed int, so 65535 is someone setting it to FF to indicate that the value isn’t set [1] standards.ieee.org/content/dam/ieee-standards/standards/web/…Frants
This is deprecatedBelamy
E
11

I need to make sure that incoming phone calls cannot interrupt the recordings that my clients make, so I prompt them to go to airplane mode but still turn on wifi. The method above from AlBeebe didn't work for me on iOS 8.1.3, but If found this solution which should work in iOS 7 and later:

You must add and import the CoreTelephony.framework.

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

Define the property on your class if you want to track changes

@property (strong, nonatomic) CTTelephonyNetworkInfo* networkInfo;  

Init the CTTelephonyNetworkInfo:

self.networkInfo = [[CTTelephonyNetworkInfo alloc] init];
NSLog(@"Initial cell connection: %@", self.networkInfo.currentRadioAccessTechnology);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(radioAccessChanged) name:CTRadioAccessTechnologyDidChangeNotification object:nil];

And then you will receive callback when it changes:

- (void)radioAccessChanged {
    NSLog(@"Now you're connected via %@", self.networkInfo.currentRadioAccessTechnology);
}

The values for currentRadioAccessTechnology are defined in CTTelephonyNetworkInfo.h and you'll get back null / nil when there is no cell tower connection.

This is where I found it: http://www.raywenderlich.com/48001/easily-overlooked-new-features-ios-7

Emetine answered 6/2, 2015 at 15:22 Comment(0)
R
10

Based on @the-guardian's response, I have come up with the following (in swift):

import CoreTelephony

/**
 Indicates if the device can make a phone call.

 - seealso: [Source](https://mcmap.net/q/183622/-detecting-whether-or-not-device-support-phone-calls)

 - returns: `true` if the device can make a phone call. `false` if not.
 */
final class func canMakePhoneCall() -> Bool {
    guard let url = URL(string: "tel://") else {
        return false
    }

    let mobileNetworkCode = CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode

    let isInvalidNetworkCode = mobileNetworkCode == nil
        || mobileNetworkCode?.count == 0
        || mobileNetworkCode == "65535"

    return UIApplication.shared.canOpenURL(url)
        && !isInvalidNetworkCode
}

This code has been tested on an iPad Air 2 Wifi, an iPad Air 2 Simulator, an iPhone 6S Plus, and seems to work appropriately. Will determine on an iPad with mobile data soon.

Readiness answered 31/5, 2016 at 19:31 Comment(1)
This one is deprecatedUnconquerable
K
2

I don't think your method is reliable, as device names may change in the future. If your concern is to prevent the app from running on non-iPhone devices, you may add the 'telephony' to the UIRequiredDeviceCapabilities dictionary in your Info.plist. That will disallow devices other than the iPhone to download your app from the App Store.

Alternatively, if what you need is checking for 3G connectivity at a particular moment, you can use Apple's Reachability utility class to ask about current 3G/WIFI connection status.

Kent answered 23/2, 2011 at 18:8 Comment(0)
V
2

I think that generally it is. I would go for a more generic string comparison (just to be safer in case of a future update). I've used it with no problems (so far...).

If you want to be more certain about whether the device can actually make calls, you should also take advantage of the Core Telephony API. The CTCarrier class can tell you whether you can actually make a call at any particular moment.

Venusberg answered 23/2, 2011 at 18:16 Comment(0)
A
2

This UIApplication.shared.openURL((URL(string: "tel://\(phoneNumber)")!)) will not say if it has SIM or Not this will just say, if the device has options to make a call. For example : A iPhone with or without SIM it'll return true, but in iPod Touch it will always return false, like wise if an ipad doesn't have sim option it will return false.

Here is the code that checks everything comprehensively ! (Using Swift 3.0)

if UIApplication.shared.canOpenURL(URL(string: "tel://\(phoneNumber)")!) {
            var networkInfo = CTTelephonyNetworkInfo()
            var carrier: CTCarrier? = networkInfo.subscriberCellularProvider
            var code: String? = carrier?.mobileNetworkCode
            if (code != nil) {
                UIApplication.shared.openURL((URL(string: "tel://\(phoneNumber)")!))
            }
            else {
                var alert = UIAlertView(title: "Alert", message: "No SIM Inserted", delegate: nil, cancelButtonTitle: "ok", otherButtonTitles: "")
                alert.show()
            }
        }
        else {
            var alert = UIAlertView(title: "Alert", message: "Device does not support phone calls.", delegate: nil, cancelButtonTitle: "ok", otherButtonTitles: "")
            alert.show()
        }

By this way, we can make sure, device supports calling or not.

Annabelle answered 16/3, 2017 at 11:49 Comment(0)
W
0

In case you are asking in order to call a phone number and show an error on devices that have no telephony:

void openURL(NSURL *url, void (^ __nullable completionHandler). (BOOL success))
{
    if (@available(iOS 10.0, *)) {
        [application openURL:url options:@{} completionHandler:^(BOOL success) {
            completionHandler(success);
        }];
    } else
    {
        if([application openURL:url]) {
            completionHandler(YES);
        } else {
            completionHandler(NO);
        }
    }
}

usage

        p97openURL(phoneURL, ^(BOOL success) {
            if(!success) {
                  show message saying there is no telephony on device

            }
        }
Whit answered 16/10, 2017 at 14:25 Comment(0)
C
-1

Based on @TheGuardian's answer, I think this might be a simpler approach:

   private final func canMakePhoneCall() -> Bool {
       guard UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone else {
        return false
       }

       let mobileNetworkCode = CTTelephonyNetworkInfo().subscriberCellularProvider?.mobileNetworkCode
       let isInvalidNetworkCode = mobileNetworkCode == nil || mobileNetworkCode?.characters.count <= 0
                                                        || mobileNetworkCode == "65535"
                                                        //When sim card is removed, the Code is 65535

       return !isInvalidNetworkCode
    }
Countershading answered 14/6, 2016 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.