如何显示 WKWebView url?

How do I display WKWebView url?

我想知道如何在 WKWebView 上显示我当前的 URL 并在更改时显示 URL。目前我添加了 Text() 但它根本不显示 URL。有什么办法可以解决吗?

WebView

import SwiftUI
import WebKit

struct WebView : UIViewRepresentable {
    
    let request: URLRequest
    private var webView: WKWebView?

    init (request: URLRequest) {
        self.webView = WKWebView()
        self.request = request
    }
    
    func makeUIView(context: Context) -> WKWebView  {
        webView?.load(request)
        return webView!
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
    
    }

    func URL() -> String {
        return (webView?.url)?.absoluteString ?? ""
    }

    func goBack() {
        webView?.goBack()
    }
    
    func goForward() {
        webView?.goForward()
    }
    
}

主视图

struct MainView: View {
   var webView: WebView = WebView(request: URLRequest(url: URL(string: "https://www.google.com")!))

 var body: some View {
  VStack {
   //...
   Text(webView.URL())
   webView
   Button(action: { webView.goBack() }, label: { Text("Test") })
//...
  }
 }

要跟踪 WKWebView 的 URL,您需要使用 WKNavigationDelegate

您可以在 UIViewRepresentable 中使用 Coordinator 作为 WKNavigationDelegate 并使用具有 @Published 值的 ObservableObject 在 [=19] 之间进行通信=] 和您的父视图:

class NavigationState : ObservableObject {
    @Published var url : URL?
}

struct WebView : UIViewRepresentable {
    
    let request: URLRequest
    var navigationState : NavigationState
    
    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }
    
    func makeUIView(context: Context) -> WKWebView  {
        let webView = WKWebView()
        context.coordinator.navigationState = navigationState
        webView.navigationDelegate = context.coordinator
        webView.load(request)
        return webView
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        
    }

    class Coordinator : NSObject, WKNavigationDelegate {
        var navigationState : NavigationState?
        
        func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
            navigationState?.url = webView.url
        }
    }
    
}

struct ContentView: View {
    @StateObject var navigationState = NavigationState()
    
    var body: some View {
        VStack(){
            Text(navigationState.url?.absoluteString ?? "(none)")
            WebView(request: URLRequest(url: URL(string: "https://www.google.com")!), navigationState: navigationState)
        }
    }
}

根据评论更新

class NavigationState : NSObject, ObservableObject {
    @Published var url : URL?
    let webView = WKWebView()
}

extension NavigationState : WKNavigationDelegate {
    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        self.url = webView.url
    }
}

struct WebView : UIViewRepresentable {
    
    let request: URLRequest
    var navigationState : NavigationState
    
    func makeUIView(context: Context) -> WKWebView  {
        let webView = navigationState.webView
        webView.navigationDelegate = navigationState
        webView.load(request)
        return webView
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) { }
}

struct ContentView: View {
    @StateObject var navigationState = NavigationState()
    
    var body: some View {
        VStack(){
            Text(navigationState.url?.absoluteString ?? "(none)")
            WebView(request: URLRequest(url: URL(string: "https://www.google.com")!), navigationState: navigationState)
            HStack {
                Button("Back") {
                    navigationState.webView.goBack()
                }
                Button("Forward") {
                    navigationState.webView.goForward()
                }
            }
        }
    }
}