sometimes in background my WKWebView app unloads - when I open it I have only WKWebView background and the app doesn't respond. I managed to catch it once - I debugged it in safari dev tools and found out at that time it had 'about:blank' url loaded instead of my app url. So I did the following:
@objc func applicationDidBecomeActive() {
generateHapticFeedback(feedbackId: 4)
let isAlive: Bool = webView.url?.absoluteString != URL(string:"about:blank")?.absoluteString
if (isAlive) {
print("[alive_check] OK, NO RELOAD NEEDED")
return
} else {
print("[alive_check] HANGED")
configurateWebView()
HTMLLoader.loadScenario(for: webView)
webView.evaluateJavaScript("alert('DEV MODE: APP HANG DETECTED. REINIT TO THE RESQUE ♀️')")
}
}
I verified my approach works fine for scenario with 'about:blank' - to test it I just set window.location.href = 'about:blank'
manually via safari dev tools. But it seems that app doesn't always have this state when unloads (this morning I have this again, and the fix doesn't help. at the same time I'm unable to connect to it with debugger after the night)
Do I have any reliable way to check that my page is still running in WKWebView and if no I will force it to reload?
I also tried to evaluate javascript to communicate to my page but it doesn't return anything.
Solutions I don't want to consider as of now:
Keep it live in background - reloading the page is completely fine to me as the app will restore it's state.
Reinitialize it every time on
applicationDidBecomeActive
as it looks a bit ugly in terms of UX.