Detecting whether or not device support phone calls?

后端 未结 9 2178
别跟我提以往
别跟我提以往 2020-11-28 23:13

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

9条回答
  •  难免孤独
    2020-11-28 23:47

    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](http://stackoverflow.com/a/11595365/3643020)
    
     - 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.

提交回复
热议问题