I need to run WKWebView in several contexts, including ones where there is no view.
In the App, it’s fine. Load it, link it, show it.
But I also need to load it in a widget – not for display, but to access the data stored in the indexeddb on the page, also for a background processing task (though that’s less critical), and finally in the handler for a pushed notification.
The issue here is I don’t want to move my entire data storage into the app – Then I’d have to duplicate the same storage for Android too – and it’s all working fine in the browser – I don’t want 3 versions if i can avoid it.
For the web-app, local storage and indexeddb storage work great.
For WKWebView, it’s great and if I need info I can request it from the page.
But WKWebView seems to need to be attached to a live view hierarchy, and cannot be attached to the widget view hierarchy.
Has anyone got a way to either instantiate a working WKWebView without attaching to the view stack, or to access the WKWebView local storage and/or indexeddb storage for a site without the full WKWebView (for example, accessing the JSContext but having it bind to a full site)
Note: I am trying to build code that can operate while offline, so I can’t just query my main site for data.
As an example (not actually what I’m building but it conveys the idea…)
Imagine an online calendar service. The app shows the calendar, allows new items etc. All appointments are cached locally in indexeddb so you can open the app and see them even if you are offline. Now you want a widget to show the calendar. It needs access to the same data. Likewise, you receive a notification of a new calendar item just before you lose signal – you open your app expecting to see it (after all, your phone’s received it), but it’s not there because the notification handler can’t access the web-context of the indexeddb
What I want is to do…
let web = WKWebView(noViewContext)
web.load("https://example.com/functionpage") // note, relying on caching here. I have that sorted)
web.evaluateJavaScript("SomeCodeHere()") { (outcome, error) in etc. etc.}