简体   繁体   English

在 SwiftUI 中使用 WKWebView 时如何拦截链接导航?

[英]How do I intercept link navigation when using WKWebView in SwiftUI?

I'm using WKWebView in a SwiftUI app.我在 SwiftUI 应用程序中使用 WKWebView。 I have an HTML file in my bundle that I'm initially loading into the WKWebView.我的包中有一个 HTML 文件,我最初将其加载到 WKWebView 中。 The file has a link in it that I would like to open in an external browser if tapped.该文件中有一个链接,如果点击该链接,我想在外部浏览器中打开该链接。 I've created the WKWebView using the following code:我使用以下代码创建了 WKWebView:

import SwiftUI
import WebKit

struct WebView: UIViewRepresentable {
    typealias UIViewType = WKWebView

    let request: URLRequest

    func makeUIView(context: UIViewRepresentableContext<WebView>) -> WKWebView {
        let webView = WKWebView()
        let delegate = WVNavigationDelegate()
        webView.navigationDelegate = delegate
        return webView
    }

    func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<WebView>) {
        uiView.load(request)
    }
}

I've created the delegate using this code:我使用以下代码创建了委托:

import Foundation
import WebKit

class WVNavigationDelegate: NSObject, WKNavigationDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        switch navigationAction.navigationType {
        case WKNavigationType.linkActivated:
            UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
            decisionHandler(.cancel)
        default:
            decisionHandler(.allow)
        }
    }
}

If I comment out the lines creating and assigning the delegate, everything works fine with the exception of links opening externally.如果我注释掉创建和分配委托的行,除了在外部打开的链接外,一切正常。 With those lines compiled in, nothing loads in the WebView.编译这些行后,WebView 中不会加载任何内容。 I put a print() statement at the beginning of the function in the delegate and it didn't even get executed.我在委托中的 function 的开头放置了一个print()语句,它甚至没有被执行。 What am I doing wrong here?我在这里做错了什么?

The navigationDelegate is weak so in the provided code it is released as soon as exit from makeUIView . navigationDelegate很弱,因此在提供的代码中,一旦退出makeUIView就会释放它。

The solution is to keep it as member, as in解决方案是将其保留为成员,如

struct WebView: UIViewRepresentable {
    typealias UIViewType = WKWebView

    let request: URLRequest
    private let delegate = WVNavigationDelegate()   // << here !!

    func makeUIView(context: UIViewRepresentableContext<WebView>) -> WKWebView {
        let webView = WKWebView()
        webView.navigationDelegate = delegate
        return webView
    }

    func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<WebView>) {
        uiView.load(request)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WKWebView 中拦截链接点击 - Intercept link clicks in WKWebView 如何阻止 SwiftUI 重新加载我通过 UIViewRepresentable 使用的 WKWebView 页面? - How do I stop SwiftUI from reloading WKWebView page that I am using via UIViewRepresentable? SwiftUI 中的 WKWebView - 用户与网站交互时如何切换视图? - WKWebView in SwiftUI - How do I switch the view when user interacts with the website? SwiftUI WKWebView 禁用链接交互 - SwiftUI WKWebView disable link interactions SwiftUI:如何更改用户按住导航链接或按钮之类的东西时发生的颜色变化? - SwiftUI: How can I change the color change that occurs when the user holds down something like a navigation link or a button? SwiftUI 如何在子视图上导航到新的导航页面 - SwiftUI How do I navigate on Child View to a new navigation page 如何在 SwiftUI 中更改导航栏标题的文本属性? - How do I change text attributes for a navigation bar title in SwiftUI? 如何在 SwiftUI 的按钮中添加导航按钮链接? - How to add navigation button link in button in SwiftUI? 如何在 SwiftUI 中按动导航链接? - How to animate the navigation link on press in SwiftUI? SwiftUI 使用导航链接“AttributeGraph 前置条件失败:访问不同命名空间中的属性”时崩溃 - SwiftUI crash when using navigation link "AttributeGraph precondition failure: accessing attribute in a different namespace"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM