Kisoth Srinathan
Kisoth Srinathan

Reputation: 115

iOS Swift call Crashes because of Invalid mobile number

This is the code I am using to make call. But it crashes because of invalid number. the number with 11 digits starts with 6 crashes but the number with 11 digits starts with 0 works fine normally with 10 digits works fine. Can anyone help?

let myurl=URL(string: "tel://\(selectedEmployeeContact)")
    let isInstalled=UIApplication.shared.canOpenURL(myurl!)
    if(isInstalled)
    {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(myurl!)
        } else {
            UIApplication.shared.openURL(myurl!)
        }
    }

Upvotes: 0

Views: 151

Answers (2)

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

Modify your code to prevent the crash:-

guard let myurl=URL(string: "tel://\(selectedEmployeeContact)") else {return}
let isInstalled=UIApplication.shared.canOpenURL(myurl)

If your myurl is nil then it will crash because it will force unwrapped the Value.

Upvotes: 1

gnasher729
gnasher729

Reputation: 52538

Question: What does the expression myurl! do if myurl is nil?

Answer: The exclamation mark will make it crash. Intentionally.

So start your debugger. Set a breakpoint, then step through each line. Check the relevant variables. Most likely you will find that myurl is nil. If not, step through the code line by line and tell us exactly where the crash happens.

Upvotes: 1

Related Questions