1

UITextView allows tappable attributedText based hyperlinks.The user interaction with the link can be intercepted by implementing the delegate method. textView:shouldInteractWithURL:in: which has two variations, for iOS 10 and one for iOS 9 as below.

// For iOS 9 and below.
@available(iOS, deprecated=10.0)
func textView(textView: UITextView, shouldInteractWith URL: NSURL, in characterRange: NSRange) -> Bool {
    // Present UIWebView here...
    return false
}

@available(iOS 10.0, *)
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    // Present UIWebView here...
    return false
}

The callback for iOS 10 works when running app in iOS 10, but when in iOS 9, the other callback never gets called. It directly launches safari.

I've tried various combinations of @available attribute but nothing worked. The delegate simply never gets called for iOS 9.

The app's Deployment Target is iOS 9 using Xcode 8 while Base SDK is iOS 10.2.

Update I'm using Swift 2.3

atastrophic
  • 3,153
  • 3
  • 31
  • 50

1 Answers1

1

Since the questioner is using Swift 2.3, we need to use its version of the method signature. I've updated the code snippet below. If you're using Swift 3, you'll want to remove URL and Range from the parameter names.

According to the docs, the only difference between the iOS 9 version and the iOS 10 version is that the iOS 10 version has the additional interaction: parameter.

// For iOS 9 and below.
@available(iOS, deprecated=10.0)
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    // Present UIWebView here...
    return false
}

@available(iOS 10.0, *)
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    // Present UIWebView here...
    return false
}
Dave Weston
  • 6,527
  • 1
  • 29
  • 44
  • I'm using swift 2.3 as of now, i think the difference in signature is because of that. I do have the additional param for iOS 10 and it does work in iOS 10 simulator as I mentioned i the question. I'll try this one too. And yes, I have used breakpoints and doesn't seem to hit either method in iOS 9 simulator or device for that matter. – atastrophic Mar 20 '17 at 04:26
  • it worked thank you so much. Although I used Xcode code completion for implementing the methods, it still provided me with `in` instead of `inRange`. – atastrophic Mar 20 '17 at 16:26