Patrick_K
Patrick_K

Reputation: 289

func textView(_ textView: UITextView, shouldInteractWith URL... not called in iOS 14

I have a UITextView with a delegate.

The delegate reacts to clicks on an URL with: textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction)

This works already with iOS 11,12 and 13 But it's not working on iOS 14. Someone an idea what might have changed?

Upvotes: 2

Views: 5473

Answers (3)

Patrick_K
Patrick_K

Reputation: 289

Just found the solution for this problem. But first thanks for all your responses. Specially from @matt and @Larme. I created an example for this problem, but it worked. So I compared with my real project and found the difference. The textView is inside a table view cell. In my real project it was added directly to the cell and not to the content view.

Old: cell.addSubview(textView)

New: cell.contentView.addSubview(textView)

Everything worked fine with the old approach, besides my problem with not opening links in iOS14. Maybe this might help someone in future.

Upvotes: 5

saqib kafeel
saqib kafeel

Reputation: 306

put these two lines in viewDidload function

textView.delegate = self
textView.isSelectable = true

Upvotes: 1

matt
matt

Reputation: 536047

Things to keep in mind:

  • The text view needs to be selectable and not editable.

  • The text view's dataDetectorTypes needs to include .link.

  • The view controller (or whoever you are) needs to adopt UITextViewDelegate, and the delegate method needs to a top level method of that view controller (not inside some other method).

  • The interaction will never be .preview (it is a dead letter).

Upvotes: 4

Related Questions