我是一个快速学习者。我使用 SwiftUI,它是一个结构体,我必须实现一个 WKWebView,并且其中的 url 正在动态更改。我必须捕捉这些不断变化的网址,但我尝试过的解决方案不起作用。
例如:https : //stackoverflow.com/a/48273950/10088243 我试过这个代码块,但它不起作用,它给了我一些编译器错误:
import SwiftUI
import WebKit
struct ContentView: UIViewRepresentable, WKNavigationDelegate {
let request = URLRequest(url: URL(string: "https://apple.com")!)
func makeUIView(context: Context) -> WKWebView {
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
preferences.javaScriptCanOpenWindowsAutomatically = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let webView = WKWebView(frame: .zero, configuration: configuration)
webView.allowsBackForwardNavigationGestures = true
return webView
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// 'override' can only …Run Code Online (Sandbox Code Playgroud)