Crash scenario with link detection in UITextView on iOS 9

Link detection Crash

In reviewing a crash log for a recently published app, I ran across a scenario where a user could reproducibly force a crash by using a long press on a link in a UITextView.

  • Occurs on iOS 9.
  • UITextView on a presented view controller.
  • Two consecutive long presses on a detected link in the UITextView.

On the first long press, I see the warning:

Attempt to present <_UIRotatingAlertController: …

The second long press causes the crash with the signature:

Assertion failure in -[UITextView startInteractionWithLinkAtPoint:]

The workaround

Some searching led me to the following thread which discussed the specific crash I was seeing (though my case was a web URL) along with some suggested workarounds:

Crash on UITextView phone number selection

I ended up implementing the shouldInteractWithURL method of the UITextViewDelegate protocol.   I return NO from this method and handle the URL myself:


- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
    if([[UIApplication sharedApplication] canOpenURL:URL]){
        [[UIApplication sharedApplication] openURL:URL];
    }
    
    // Don't let UITextView handle the URL interaction.
    return NO;
}

Problem solved.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s