0

Voice over doesn't detect links in UITextView (iOS 13)

I have below code which works fine for voice over on iOS 12 (i.e. Voice over selects individual links on swipe up/down) but on iOS 13 swipe up/down doesn't work

class ViewController: UIViewController {

@IBOutlet weak var myTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let text = "Testing https://www.google.com/ in link and 904-567-5678 is phone number"
        let attributedString = NSMutableAttributedString(string: text)

        myTextView.attributedText = attributedString
        myTextView.isUserInteractionEnabled = false
        myTextView.isEditable = false
        myTextView.isSelectable = true
        myTextView.dataDetectorTypes = [.link,.phoneNumber]
        myTextView.font = UIFont(name: myTextView.font!.fontName,
                                 size: 25.0)
        myTextView.backgroundColor = .red
    }


    func textView(_ textView: UITextView,
                  shouldInteractWith URL: URL,
                  in characterRange: NSRange,
                  interaction: UITextItemInteraction) -> Bool {

        UIApplication.shared.open(URL, options: [:])
        return false
    }

}
Akhil Shrivastav
  • 427
  • 3
  • 13

2 Answers2

0

If this still isn’t working in iOS 13, as a workaround, you could do:

attributedString.addAttribute(.link, value: myLinkURL, range: NSRange(myLinkLocation...)

Alex Walczak
  • 1,276
  • 1
  • 12
  • 27
0

Please see my answer here: https://stackoverflow.com/a/66155952/4600723

The UITextView on iOS 13+ behaves differently than iOS 12. The fix I am using is to set isEditable = true and then the links are correctly detected for Voice Over users (for non-VO users you need to handle the delegate method to keep UITextView uneditable.)

Marek Staňa
  • 522
  • 7
  • 10