我有工作链接的代码和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
}发布于 2017-12-31 12:12:27
您所看到的错误是因为您试图在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
}https://stackoverflow.com/questions/48041947
复制相似问题