WKWebView displays white screen after the application goes into the background

I have a project that must upload data in background and keep data going even when the app is closed/terminated. In very rare cases I have encountered the problem that when background uploading is in process (app was fully closed/terminated) and when I open the application a white (blank) screen appears. No splash screen, no content in webview, but I still can see my logs that the background tasks is producing. After the completion of data uploading the white screen does not go away, obviously.

My application is built React on gets loaded in a WebView. I noticed one thing, although I see only a white screen, according to the inspector the DOM is fully loaded and functional and the connection between React app and native app is still there (callbacks, facades). Reloading page or opening another page doesn't have any positive effect. Also I am able to highlight elements using Safari's inspector and can visually observe their outlines on the end device. They're present, but content doesn't renders at all.

The problem lies precisely because of the incorrect operation of WebKit when the application goes into the foreground during background tasks active, after when the user swipes away the application and the system terminates the application (app termination is an important condition).

To solve this problem I used the following:

  1. An attempt to catch the system closing Webkit and then reload React App. I implemented this method

func webViewWebContentProcessDidTerminate(_ webView: WKWebView) However I never get into this method (unless I destroy the process associated with webkit in the emulator myself). Obviously process doesn't gets terminated and it's not the cause. 2. Trying to resize the WebView doesn't solve the problem either. 3. I tried to do webView.reload() after the application gets into willEnterForeground, but this does not always work correctly and does not completely solve the problem. Sometimes the webView is rendered, and sometimes there is still a white screen even though the method was called. 4. I tried calling webView.loadHTMLString on a "broken" webview just to make sure that React app is not the cause and it still doesn't render anything, yet i can see the new content from inspector but white screen still stay. 5. Complete recreation of WKWebView (removing the old one from WebViewContainer, creating and adding the new one, loading react app) is solving the problem, but it is a pretty much rough workaround, similiar to app restarting. And also I find it difficult to find a right moment/event to apply this workaround.

Are there any known related issues or any known causes of this? What can be putting webview into this weird state?

My app has also recently (past ~3 weeks) been seeing this issue happening as well. This could be a regression caused by a recent iOS change, I've added a bug report using Feedback, https://feedbackassistant.apple.com/feedback/13350093

A useful debugging tip for others running into this: here is how you can locally destroy the WebView and have that webViewWebContentProcessDidTerminate get called: https://gist.github.com/jasonbekolay/dad7c446ae1b02f174dc3eb3a5ea70ee

@applesauce92, I follow the link, but it doesn't work. Feedback not found. Is there any known workaround/best practice to fix this problem? Like detecting it happening or avoiding it somehow. Have you managed to beat it on real device?

@applesauce92 , I follow the link https://feedbackassistant.apple.com/feedback/13350093, but it doesn't work. Feedback not found.Is there any known workaround/best practice to fix this problem? Like detecting it happening or avoiding it somehow. Have you managed to beat it on real device?

@ElissP We have not found a workaround but I'll post here if we find one. Right now what I'm trying to do is prevent the process from being terminated in the background by reducing the memory usage of the WebView. One way I'm doing this is following the suggestion here: https://stackoverflow.com/a/32443423 and calling https://developer.apple.com/documentation/webkit/wkusercontentcontroller/3585114-removeallscriptmessagehandlers?language=objc when the app is backgrounded, and restoring it when the app is foregrounded,

I want to mention that for months now, before this bug started happening in our app (I think), this delegate function was getting called webViewWebContentProcessDidTerminate BUT the blank screen bug did not happen.

I am guessing this means that the OS would resume / reload the WebView process on a foreground for us, but now the OS is not doing this so webViewWebContentProcessDidTerminate is still called but the OS is not restoring the WebView, hence the blank screen issue.

Perhaps a recent OS change removed this functionality and now the OS is not restoring the WebView process upon webViewWebContentProcessDidTerminate?

WKWebView displays white screen after the application goes into the background
 
 
Q