Trouble with a WKWebView Delegate in SwiftUI on iOS

I'm building an app that has two separate web views. When you click on a list of articles in a scrolling view, a corresponding web page is loaded into the first view. I'm trying to get it so that when I click on a link in that view, it loads that link into the second. This is what I'm using to create a web view:

struct LogWebView: UIViewRepresentable { typealias UIViewType = WKWebView @ObservedObject var theLibraryComputer = ALibraryComputer.shared let url: URL? private let delegate = WVNavigationDelegate()

func makeUIView(context: Context) -> WKWebView {
    let prefs = WKWebpagePreferences()
    prefs.allowsContentJavaScript = true
    let config = WKWebViewConfiguration()
    config.defaultWebpagePreferences = prefs
    let webView = WKWebView()
    return webView
}

func updateUIView(_ uiView: WKWebView, context: Context) {
    guard let myURL = url else {
        return
    }
    let request = URLRequest(url: myURL)
    uiView.load(request)
}

}

Right now, the first web view is driven by a variable stored in an Observable Object. That variable gets set when the user clicks on a link, and the web view loads just fine. To fix the "loading to a second view" problem I thought that a delegate could be used to intercept the link action. I'm using this delegate:

class WVNavigationDelegate: NSObject, WKNavigationDelegate { @ObservedObject var theLibraryComputer = ALibraryComputer.shared

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)
    }
}

}

But when I tie that delegate into my WebView, thusly, things break:

func makeUIView(context: Context) -> WKWebView { let prefs = WKWebpagePreferences() prefs.allowsContentJavaScript = true let config = WKWebViewConfiguration() config.defaultWebpagePreferences = prefs let webView = WKWebView()

    webView.navigationDelegate = delegate

    return webView
}

Now the webview is no longer loading when I click on a link in my scrolling list. (And yes, I know that this delegate won't actually solve my navigation problem but I can't even see that problem until this is fixed.) Thanks!

Trouble with a WKWebView Delegate in SwiftUI on iOS
 
 
Q