How to share JS context/runtime between multiple WKWebViews?

Viewed 21

The problem

I have two WKWebView loading local HTML files, where the first contains some JS to load a widget and display further HTML on a <div>, and the second only contains a <div> anchor point.

I'd like the JS running in the first WKWebView to be running on the second, so that it can load further HTML on the <div> as well.

Both are loaded as UIViewRepresentable in SwiftUI.

What I have tried

So far I have tried using the same WKProcessPool with a shared singleton however although I can see that the cookies are being shared, the JS environment is not, so the HTML is only being added to the anchor point on the first HTML file, containing the initial JS.

Potential solutions at the moment are using and then reconfiguring a single, shared WKWebView, but this is not ideal as I will only be able to display one at a time. Running the JS on both is not an option as it will overreport analytics.

I hope this makes sense and it'd be great if anybody had any suggestions.

0 Answers
Related