gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:56:02 27 4
gpt4 key购买 nike

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

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

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

我该如何解决这个问题?


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

'NSInvalidArgumentException', reason: '指定的 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
}

最佳答案

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

所以我假设您想在 Safari View Controller 中打开链接并使用其他东西打开电子邮件和电话号码。

首先改回处理所有链接然后做这样的事情:

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
}

关于ios - 在 TextView 中单击电话号码或电子邮件地址时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com