Detecting whether or not device support phone calls?

后端 未结 9 2180
别跟我提以往
别跟我提以往 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条回答
  •  旧时难觅i
    2020-11-28 23:47

    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.

提交回复
热议问题