【问题标题】:Error when click on a phone number or email address in TextView在 TextView 中单击电话号码或电子邮件地址时出错
【发布时间】:2018-06-11 00:52:18
【问题描述】:

我有工作链接的代码,可以编辑 TextView。

链接会在应用程序中打开。

链接有效,但(邮件地址、电话号码)无效

我该如何解决这个问题?


点击电话号码或电子邮件地址时出错:

'NSInvalidArgumentException',原因:'指定的 URL 具有不受支持的方案。仅支持 HTTP 和 HTTPS URL。'


import SafariServices

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    // Open links with a SFSafariViewController instance and return false to prevent the system to open Safari app
    let safariViewController = SFSafariViewController(url: URL)
    present(safariViewController, animated: true, completion: nil)
    return false
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}
@objc func viewTapped(_ aRecognizer: UITapGestureRecognizer) {
    self.view.endEditing(true)
}
// when you tap on your textView you set the property isEditable to true and you´ll be able to edit the text. If you click on a link you´ll browse to that link instead
@objc func textViewTapped(_ aRecognizer: UITapGestureRecognizer) {
    viewText.dataDetectorTypes = []
    viewText.isEditable = true
    viewText.becomeFirstResponder()
}
// this delegate method restes the isEditable property when your done editing
func textViewDidEndEditing(_ textView: UITextView) {
    viewText.isEditable = false
    //viewText.dataDetectorTypes = .all
    viewText.dataDetectorTypes = .link
}

【问题讨论】:

    标签: ios swift hyperlink


    【解决方案1】:

    您看到的错误是因为您试图在 Safari 中打开电子邮件或电话号码,但它无法处理此类方案。

    所以我假设你想在你的 Safari 视图控制器中打开链接并用其他东西打开电子邮件和电话号码。

    首先改回处理所有链接,然后执行以下操作:

    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        if (URL.scheme?.contains("mailto"))! {
            // Handle emails here
        } else if (URL.scheme?.contains("tel"))! {
            // Handle phone numbers here
        } else if (URL.scheme?.contains("http"))! || (URL.scheme?.contains("https"))! {
            // Handle links
            let safariViewController = SFSafariViewController(url: URL)
            present(safariViewController, animated: true, completion: nil)
        } else {
            // Handle anything else that has slipped through.
        }
    
        return false
    }
    

    【讨论】:

    • 非常感谢)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    相关资源
    最近更新 更多