UITextView startInteractionWithLinkAtPoint crash iOS 11 only

感情迁移 提交于 2020-01-09 10:48:07

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/46732546/uitextview-startinteractionwithlinkatpoint-crash-ios-11-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!