我有一个聊天应用程序,它偶尔会收到一个链接。单击该链接后,我可以在Safari中查看网页,我想改为在我的应用程序中的UIWebview中打开该链接。
我正在经历一个this article,这一切都很好,但它给了一个硬编码的链接。我想能够打开用户刚刚点击的链接。实现这一目标的最佳方法是什么?

最佳答案

基本上,您需要首先让textView识别url

textView.text = "example www.google.com"
textView.isSelectable = true
textView.dataDetectorTypes = UIDataDetectorTypes.link

但是在delegate methodshouldInteractWithURL中,返回false,这样您的URL是可单击的,但不会跳转到Safari。
extension ViewController: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        print(url)
        //load url to YOUR web view here!
        return false
    }
}

别忘了设置代表!
textView.delegate = self

09-30 11:55
查看更多