简体   繁体   中英

swift3 - canOpenURL: failed for URL tel

I want to intent a call , this is my code :

if let urlMobile = NSURL(string: "tel://076938483"), UIApplication.shared.canOpenURL(urlMobile as URL) {

                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(urlMobile as URL, options: [:], completionHandler: nil)
                }
                else {
                    UIApplication.shared.openURL(urlMobile as URL)
                }
            }

I'm using swift 3 to do so but I get this error:

-canOpenURL: failed for URL: "tel://09178883828" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

any idea to do so ?

Your code works perfectly fine. Run it on an actual device if your using Simulator. You can't simulate a call on a Mac/MacBook.

Please have a look at Simulator Hardware Actions in Apple documentation.

Some LSApplicationQueriesScheme do not work on Simulator. Error Code -10814 is for kLSApplicationNotFoundErr. Simulator can't launch Dial Pad for Telephone. So run it on iPhone device.

This worked for me!!!

Code Should be

if let url = NSURL(string: "tel://\(yourNumber)"), UIApplication.shared.canOpenURL(url as URL) {
  UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)
}

网址应该是:

if let urlMobile = NSURL(string: "tel:///076938483"), UIApplication.shared.canOpenURL(urlMobile as URL) {
let phonenumber = "076938483"
guard let url = URL(string: "tel://\(phonenumber )") else {
            return
            
        }
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }

In the else part of the URL generation, print anything, if it is getting printed, you need to check the format of the phone number.

@IBAction func Call(_ sender: Any) {

    let busPhone = "7355535586"
    if let url = URL(string: "tel://\(busPhone)"), UIApplication.shared.canOpenURL(url) {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }
    }    
@objc func callBtn() {
       let userPhone = String((phoneNum.filter {!" \n\t\r".contains($0)}))
       if let url = URL(string: "tel://\(phoneNum)"), UIApplication.shared.canOpenURL(url) {
           DispatchQueue.main.async {
               UIApplication.shared.open(url)
           }
        }
    }

Note : This code can work when you run with the real device not simulator. And don't forget add LSApplicationQueriesSchemes in your info.plist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM