UITextView 超链接到内部 WebView

UITextView Hyperlink to Internal WebView

UITextView 允许基于 hyperlinks.The 的可点击 attributedText 用户与 link 的交互可以通过实现委托方法来拦截。 textView:shouldInteractWithURL:in: 有两种变体,iOS 10 和一种 iOS 9,如下所示。

// For iOS 9 and below.
@available(iOS, deprecated=10.0)
func textView(textView: UITextView, shouldInteractWith URL: NSURL, in characterRange: NSRange) -> Bool {
    // Present UIWebView here...
    return false
}

@available(iOS 10.0, *)
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    // Present UIWebView here...
    return false
}

iOS 10 的回调在 iOS 10 中的 运行 应用程序时有效,但在 iOS 9 中时,另一个回调永远不会被调用。它直接启动野生动物园。

我尝试了 @available 属性的各种组合,但没有任何效果。代表永远不会被要求 iOS 9

应用程序的 Deployment TargetiOS 9 使用 Xcode 8Base SDKiOS 10.2

更新 我正在使用 Swift 2.3

由于提问者使用的是Swift2.3,我们需要使用其版本的方法签名。我已经更新了下面的代码片段。如果您使用 Swift 3,您需要从参数名称中删除 URLRange

根据文档,iOS 9 版本和 iOS 10 版本之间的唯一区别是 iOS 10 版本具有额外的 interaction: 参数.

// For iOS 9 and below.
@available(iOS, deprecated=10.0)
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    // Present UIWebView here...
    return false
}

@available(iOS 10.0, *)
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    // Present UIWebView here...
    return false
}