Search code examples
iosswift3openurl

Programmatically make phone call crash in swift 3


Hello I am making a phone call programmatically. The phone number I am receiving from the back end is like 61234567890. It doesn't comes with the +. This is how I open the call.

self.openAppURL(strUrl: "tel://\(selectedEmployeeContact)")

func openAppURL(strUrl:String)
{
    let myurl=URL(string: strUrl)
    let isInstalled=UIApplication.shared.canOpenURL(myurl!)
    if(isInstalled)
    {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(myurl!)
        } else {
            UIApplication.shared.openURL(myurl!)
        }
    }

}

I have a list of phone numbers. If number have 10 digits then it doesn't crash. but this number it crashing. Why its not handling by the canOpenURL. How to handle this properly.

Please help me.


Solution

  • You can use guard let for prevent a crash.

        guard let number = URL(string: "tel://61234567890") else { return }
        UIApplication.shared.open(number)
    

    This code is working fine more me.

    enter image description here