6

I'm facing a weird bug in my iOS app, and it only happens in device. In my app I've a home page, from there if user presses a button I'll show a FormSheet (About Us page).

let storyBoard = UIStoryboard(name: "Utility", bundle: nil);
let aboutUsVC  = storyBoard.instantiateViewControllerWithIdentifier("AboutUs") as! AboutUsViewController;
aboutUsVC.modalPresentationStyle = .FormSheet;
aboutUsVC.preferredContentSize   = CGSize(width: 500,height: 400);
self.presentViewController(aboutUsVC, animated: true, completion: nil);

I've placed a UITextView inside that about us page and added a link as it's content: Link in Text View

Issue 1

When I long press on that link I'm getting a warning message on my console:

<_UIRotatingAlertController: 0x13e107200> on which is already presenting

Issue 2

After the long press if I click on the link again, the app crashes with following message:

2016-03-16 18:11:37.022 MyApp[938:400786] *** Assertion failure in -[UITextView startInteractionWithLinkAtPoint:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.29.5/UITextView_LinkInteraction.m:377

2016-03-16 18:11:37.023 MyApp[938:400786] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '' *** First throw call stack: (0x184220f48 0x198e47f80 0x184220e18 0x185114a1c 0x18a12de50 0x189d38be4 0x189d2f330 0x189958b5c 0x1897e685c 0x189d3070c 0x1897a58b8 0x1897a263c 0x1841d7bd0 0x1841d5974 0x1841d5da4 0x184104ca0 0x18f184088 0x18981cffc 0x100188368 0x19968a8b8) libc++abi.dylib: terminating with uncaught exception of type NSException

I think the UIKit is causing that crash. How can I fix this crash ?

More Info:

  1. Base SDK : 9.2
  2. Deployment Target : 8.1
  3. Xcode Version : 7.2.1
  4. iOS Device OS Version : 9.1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • see this link may be it helps you http://stackoverflow.com/questions/32653268/error-on-textview-with-detection-phone-number-ios-9 – Anbu.Karthik Mar 16 '16 at 13:54
  • its look like bug : https://github.com/jessesquires/JSQMessagesViewController/issues/1247 – Nitin Gohel Mar 16 '16 at 14:13
  • 1
    it's a bug with iOS9: https://forums.developer.apple.com/thread/19480 I had to do what is listed in that forum post (and also what is mentioned in the answers that @Anbu.Karthik has linked) – Louis Tur Mar 16 '16 at 14:42

4 Answers4

6

As mentioned in this Apple Forum post, I implemented the following UITextViewDelegate and it solved my issue

func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
{
    UIApplication.sharedApplication().openURL(URL)
    return false  
}  

@Louis Tur: Thanks for the link

Swift 5.2

 @available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool 
{
    UIApplication.shared.openURL(url)
    return false  
}

@available(iOS, deprecated: 10.0)
func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange) -> Bool 
{
   UIApplication.shared.openURL(url)
   return false  
}
brainray
  • 12,512
  • 11
  • 67
  • 116
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 1
    I had the delegate method implemented but I was returning true. Returning false fixed the problem. Thank you! – Mr Stanev Sep 27 '17 at 19:54
2

My fix for the crash was to implement both the deprecated and its replacement delegate methods.

/// Gets called if iOS version is >= 10.
@available(iOS 10.0, *)
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    return textViewShouldInteractWithURL(URL: URL)
}

/// deprecated delegate method. Gets called if iOS version is < 10.
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
    return textViewShouldInteractWithURL(URL: URL)
}

func textViewShouldInteractWithURL(URL: URL) -> Bool {
    // common logic here
}
Victor Bogdan
  • 2,022
  • 24
  • 33
1

We've seen this issue in PSPDFKit as well and after investigating the UIKit assembly and WKWebView sources we found a workaround that is still horrible, but not invasive.

The main strategy is to be selective and apply a workaround just in time - then clean up again. You can read the source code here:

https://gist.github.com/steipete/b00fc02aa9f1c66c11d0f996b1ba1265

And please dupe rdar://26295020 so this will get hopefully fixed in time for iOS 10. (The bug exists since iOS 8 and was first reported on iOS 8b5.)

This is potentially a better solution than changing the behaviour of how URLs are interacted with.

steipete
  • 7,581
  • 5
  • 47
  • 81
1

I faced this problem when used incorrect URL format in my links. I wanted to present a new controller on screen upon tapping a link and implemented a custom URL scheme to support it. When doing so, I mistakenly wrote the scheme in format scheme:\\ instead of scheme:// and caught the crash.

Zmicier Zaleznicenka
  • 1,894
  • 1
  • 19
  • 22