简体   繁体   中英

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?

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)
    }
}

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM