【发布时间】: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
}
【问题讨论】: