2

I am not able to open the NSURL inside the UITextView with Accessibility turned ON.(Working fine when turned OFF)

I have attributed text inside the UITextView that contains some string and some NSURLs.

Please note I have used attributed string as I need the URLs to be underlined and shown as string only, so that when the user clicks on that underlined string, the link is opened in the browser.

Please let me know if more clarification is required.

P.S. I had already used UITapGesture but didnt succeed.

CGPoint tapLocation = [tapGesture locationOfTouch:0 inView:textView];
UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];

NSURL *url = attributes[NSLinkAttributeName];

This textPosition is not correct when Accessibility is ON.

Thanks

Nipun Arora
  • 372
  • 1
  • 10
  • 1
    a tap gesture is less likely to be used by someone with accessibility turned on. have you enabled the link to work if the user swipes through the text to it and double taps? does the link describe itself as a link in voiceover? – Wain Jan 20 '16 at 17:29
  • yes it says itself as a link. One more thing, this solution is working fine in simulator. – Nipun Arora Jan 20 '16 at 17:31
  • If its describing itself as link than by swiping up or down focus will immediately goes to the link and than user double taps and action will gets triggered which is basically the delegate of your UITextView. – Anshul Sep 02 '16 at 07:30
  • @NipunArora, did you find any solution for accessing links in uitextview ? – Saif Oct 30 '17 at 12:15

1 Answers1

2

You can detect Links by enabling UITextView dataDetectorTypes.

let yourString = "<html><body><p>Hi Nipun. <a href= http://www.google.com>Visit Google</a></p></body></html>"

    do{
        let attr = try NSAttributedString(data: yourString.dataUsingEncoding(NSUTF16StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType], documentAttributes: nil)
        myTextView.attributedText = attr

    }catch{

    }

    myTextView.delegate = self
    myTextView.editable = false
    myTextView.tintColor = UIColor.redColor()
    myTextView.dataDetectorTypes = UIDataDetectorTypes.Link

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    /*
    let s = URL.absoluteString

    let text2 = s.stringByReplacingOccurrencesOfString("%20", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

    print(text2)

    myWebView.loadRequest(NSURLRequest(URL: URL))
    */
    return true // Return False if you want to load URL in webview else it will redirect to Safari
}

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • shouldInteractWithURL is not getting called when Accessibility is ON. – Nipun Arora Jan 21 '16 at 15:10
  • @NipunArora If you swipe up or swipe down it will take you to the link than you can double tap and yours shouldInteractWithURL method will be called. – Anshul Sep 02 '16 at 07:36
  • @IOSCODER, swiping up/down is taking to the link, but double tapping is not calling `shouldInteractWithURL`, nothing happens? – Saif Oct 30 '17 at 12:22