I am having a chat application, which occasionally receives a link. On clicking that link, I can view the webpage in Safari, I would like to open that link in a UIWebview in my app instead.
I was going through this article, And its all good but it is giving in a hard coded link. I want to be able to open the link that user just clicked upon. whats the best way to achieve this?
Basically, you need to first ask your textView
to recognize urls
textView.text = "example www.google.com"
textView.isSelectable = true
textView.dataDetectorTypes = UIDataDetectorTypes.link
But then in delegate method shouldInteractWithURL
, return false so that your URL is clickable but you will not jump to 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
}
}
Don't forget to set delegate!
textView.delegate = self