Is webViewWebContentProcessDidTerminate ever called for an app running in the background?

Viewed 428

If a WKWebView terminates when an app is running in the foreground, webViewWebContentProcessDidTerminate will be called on its delegate.

But what happens if the app is in the background?

Will it still receive the callback immediately? Will it receive it when the app is later foregrounded? Does it not run at all?

I currently don't know how to trigger a termination – otherwise I would just try it.

I ask because I want to make it reload the web view that was terminated to avoid a blank screen in my app, but I'm not sure if this will actually work with an app running in the background.

2 Answers

For me WKWebView terminates only if my app is in the background and iOS lacks memory for other apps terminating the WKWebView in my app. If you open the app again, WKWebView is broken and webViewWebContentProcessDidTerminateis called.

Try to reproduce as @joemasilotti pointed:

  1. Open your app with a WKWebView active
  2. Move your app in the background - don't terminate it!
  3. Open a lot of other apps to use memory or maybe play a graphic intense game for a view seconds.
  4. Try to open your app again and you should see a blank/grey screen.

If you're running a simulator, you can kill the web content process from the command to trigger a webViewWebContentProcessDidTerminate call:

# In the command line, find the PID of your simulator process:
ps -p `pgrep launchd_sim`

# or if you have many simulators running:
ps -A | grep launchd_sim

# Find the PID of the WebContent process:
pgrep -P <simulator-pid> 'com.apple.WebKit.WebContent'

# kill it
kill <webcontent-pid>

# or if you want a one-lner:
kill $(pgrep -P $(pgrep launchd_sim) 'com.apple.WebKit.WebContent')

gist here: https://gist.github.com/jasonbekolay/dad7c446ae1b02f174dc3eb3a5ea70ee

Related