UITextView startInteractionWithLinkAtPoint仅崩溃iOS 11

因此,当用户与URL链接进行交互时,我在UItextview中遇到崩溃。 所有崩溃报告仅限iOS版本11。 这看起来像iOS 9中众所周知的bug,但是没有单个报告iOS版本低于11,而且在报告中我发现有趣的行:

UITextGestureClusterLinkInteract smallDelayRecognizer: 

随附iOS 11( http://developer.limneos.net/?ios=11.0&framework=UIKit.framework&header=UITextGestureClusterLinkInteract.h )。 无论如何,现在我用它来修复它

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

这不是很酷,因为你丢失了动作表菜单。 我的假设是它是由3D触摸引起的(比如先前版本中的长按),但是如果我检测到3D触摸(75%,甚至是最大力的50%)并禁用此特定手势的链接交互 – 问题仍会出现。 有没有人对这个特殊问题有一些经验和更优雅的解决方法?

崩溃是由iOS 11.0和11.1上的UIDragInteraction / UITextDragAssistant引起的。 它在iOS 11.2上修复。 对UITextView进行子类化并禁用拖放手势可以防止任何iOS版本崩溃:

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

我经历并解决了这个问题。

这是我的生产代码中的解决方法。 禁用交互.preview状态,因为崩溃只发生此状态。

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