Post marked as solved
Post marked as solved with 3 replies, 1,002 views
I have a WKWebView wrapped in a UIViewRepresentable. I want to modify certain URLs during page navigation, but the code for the delegate never gets called...
import SwiftUI
import WebKit
struct SwiftUIWebView: UIViewRepresentable {
let url: URL
func makeUIView(context: Context) -> WKWebView {
return WKWebView()
}
func updateUIView(_ webView: WKWebView, context: Context) {
let request = URLRequest(url: url)
webView.load(request)
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
}
extension SwiftUIWebView {
@MainActor class Coordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
print("Delegate called...")
webView.uiDelegate = self
webView.navigationDelegate = self
if let url = navigationAction.request.url, url.host != "somedomain.com", await UIApplication.shared.open(url) {
return .cancel
} else {
return .allow
}
}
}
}
I never see the text in the console, and the behavior is not what I expect. What am I missing? 🤔