Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView startInteractionWithLinkAtPoint crash iOS 11 only

So I experience crash in UItextview while user interacts with URL link there. All crash reports have iOS version 11 only. This looks like well-known bug in iOS 9, but there is no single report iOS versions lower than 11, and also in report I found interesting line:

UITextGestureClusterLinkInteract smallDelayRecognizer:

which came with iOS 11 (http://developer.limneos.net/?ios=11.0&framework=UIKit.framework&header=UITextGestureClusterLinkInteract.h). Anyway, for now I fixed it with

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

which is not so cool, because you lose action sheet menu. My assumption was that it is caused by 3D touch (like by long press in previous versions), but if I detect 3D touch (75%, or even 50% of maximum force) and disable link interaction for this specific gesture - issue still appears. Does anybody has some experience with this particular issue and more elegance way of solving it?

like image 980
Yurii Koval Avatar asked Oct 13 '17 14:10

Yurii Koval


2 Answers

A crash is caused by a UIDragInteraction/UITextDragAssistant on iOS 11.0 and 11.1. It is fixed on iOS 11.2. Subclassing your UITextView and disabling drag and drop gestures will prevent the crash for any iOS version:

class NoDragDropTextView: UITextView {

    override func addGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {

        // required to prevent drag and drop gestures
        // also prevents a crash on iOS 11.0-11.1
        gestureRecognizer.isEnabled = false

        super.addGestureRecognizer(gestureRecognizer)
    }
}
like image 131
Cœur Avatar answered Nov 18 '22 08:11

Cœur


I experienced and solved this problem.

This is workaround in my production code. Disable interaction .preview state because of crashes only this state occurs.

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    switch interaction {
    case .presentActions, .invokeDefaultAction:
        return handleLinkURL(url: URL)
    case .preview:
        return false
    }
}
like image 2
Motoki Narita Avatar answered Nov 18 '22 06:11

Motoki Narita