簡體   English   中英

swift3 - canOpenURL:URL 電話失敗

[英]swift3 - canOpenURL: failed for URL tel

我想打個電話,這是我的代碼:

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)
                }
            }

我正在使用 swift 3 來執行此操作,但出現此錯誤:

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

有什么想法嗎?

您的代碼運行良好。 如果您使用模擬器,請在實際設備上運行它。 您無法在 Mac/MacBook 上模擬通話。

請查看 Apple 文檔中的Simulator Hardware Actions

某些 LSApplicationQueriesScheme 在模擬器上不起作用。 錯誤代碼 -10814 用於 kLSApplicationNotFoundErr。 模擬器無法啟動電話撥號盤 所以在 iPhone 設備上運行它。

這對我有用!!!

代碼應該是

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)
        }

在 URL 生成的 else 部分,打印任何內容,如果打印出來,您需要檢查電話號碼的格式。

@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)
           }
        }
    }

注意:當您使用真實設備而不是模擬器運行時,此代碼可以工作。 並且不要忘記在 info.plist 中添加 LSApplicationQueriesSchemes。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM