单击TextView中的电话号码或电子邮件地址时出错

问题描述 投票:1回答:1

我有工作链接的代码,可以编辑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个回答
2
投票

您看到的错误是因为您尝试在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
}
© www.soinside.com 2019 - 2024. All rights reserved.