WKWebView doesn't run JavaScript when on background

I noticed that sometimes when the webview is moved to background (have another view controller on top), it stops JavaScript functions execution, and executes them only when returning back to foreground.

I read about people having the same problems with socket.io, and most of the solutions was some kind of hack, like adding the webview to the key window, or looping dummy calls to evaluateJavaScript so the webview wouldn't go to idle mode and stop JavaScript.

Those solutions work btw, but I don't like them at all.

I was wondering if there was a better solution for this problem, any way to configure the webView to prevent going into idle mode, or more elegant way to prioritize JavaScript.

Thanks.

Replies

Did you find any solution to this? I don't need to run in the background, but just need a websocket connection to work reliably for a headless WKWebView. I can only get it to work if the webview has a parent view, and I am not even sure that is dependable.

Hey guys, any progress on this? I am experiencing a similar problem where a beacon function (JavaScript) in the previous WKWebView is interrupted once a new one is presented on top.