Thanks @BabyJ, but unfortunately, that doesn't work b/c "self" in this context is a NSObject and can't be assigned to a property of type WKNavigationDelegate.
I've refactored from a struct to a class, and the compiler's not complaining, but it's still not working. I feel I'm grasping at straws at this point. Any other insights would be appreciated. Here's what I've got now...
final class SwiftUIWebView: NSObject, WKUIDelegate, UIViewRepresentable, WKNavigationDelegate {
let url: URL
override init() {
self.url = URL(string: "https://mydomain.com/page")!
}
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.navigationDelegate = self
webView.uiDelegate = self
return webView
}
func updateUIView(_ webView: WKWebView, context: Context) {
let request = URLRequest(url: url)
webView.load(request)
}
func makeCoordinator() -> Coordinator {
print("Coordinator called")
return Coordinator()
}
@MainActor class Coordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
print("Delegate called...")
if let url = navigationAction.request.url, url.host != "mydomain.com", await UIApplication.shared.open(url) {
return .cancel
} else {
return .allow
}
}
}
}