BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

Re-using a WkWebView in multiple SwiftUI views

Forums > SwiftUI

Hi, I am building an app that is a hybrid of SwiftUI and a Single Page Web App (SPA). The SPA will be displayed in a WkWebView, wrapped into SwiftUI using a UIViewRepresentable. It is not always visible, but I can guarantee that only one instance of the web view will be visible at a time.

Since the SPA takes some time to load (due to a heavy JavaScript bundle) and initialize, I'd prefer setting up one single WkWebViews and re-use that whenever SwiftUI needs to display it. I tried this approach, by initializing the WkWebView in a view model and just returning the same instance whenever makeUIView is called in the UIViewRepresentable:

func makeUIView(context: Context) -> WKWebView {
        return viewModel.webView
}

The Coordinator is also constructed when setting up the viewModel. The viewModel itself is initialized in the @main view struct of the app and handed over as using .environment(), so both the webView and the Coordinator are available when SwiftUI shows the web view.

However, this approach causes a crash once the user tries to scroll through the page.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: targetNode'

It seems as if some SwiftUI internals get messed up with this approach.

Is there another way to re-use a single WkWebView across multiple SwiftUI views?

1      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.