Henrik N
Henrik N

Reputation: 16294

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

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.

Upvotes: 13

Views: 4446

Answers (3)

Peer Mohamed Thabib
Peer Mohamed Thabib

Reputation: 705

Adding my answer to the questions asked so anyone can benefit from this.

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?

Did a quick test and the Answer is YES, received the callback immediately even when the app was in the background

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

The previous answers to this question are correct, I would like to add my findings on top of it.

If you are someone like me, who hates Command Line here is the GUI-based solution.

Step 1. Run the app on the simulator
Step 2. Open Activity Monitor from Mac's Launchpad (refer to the below screenshot)

Activity Monitor Sample

Step 3. Select the CPU tab from the top menu
Step 4. Search webcontent on the right-hand side top corner search bar (refer to the attached screenshot)

Step 3 & 4 screen reference

Step 5. As soon as you select all filtered processes the stop button will be enabled.

Process Selection

Step 6. Press the stop button it will open a popup(refer to the below screenshot)

Stop process sample

Step 7. Press the quit button - Voila! the WebView triggers the 

webViewWebContentProcessDidTerminate

If you are someone like me who wants to test it on a real device, Unfortunately, the Activity Monitor is not available on iOS devices (AFAIK Add your comments if there is any other option).

Fret not, you can follow the ways I followed to simulate on the real device.

Try to find apps that use WebView-based content rendering.

For example, the Gmail app. Open every email that you have in your inbox (Useful Tip: Swipe left and right to navigate between previous and next emails to make the process faster) and use it for some time (The best option would be content-heavy newsletters)

Another option would be to use the Safari browser on your device. Use it for some time you will be able to simulate.

NOTE 1: Make sure you are not running multiple apps in the background then the OS will terminate your app in the background state. When you reopen your app it will be launched freshly, and your efforts will be wasted.

NOTE 2: Try to do it on the lower-end device as it is easier to simulate.

I hope this answer is helpful, Happy debugging!

Upvotes: 5

Jason B
Jason B

Reputation: 406

If you're running a simulator, you can kill the web content process from the command line 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

Upvotes: 21

Fa11enAngel
Fa11enAngel

Reputation: 4810

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.

Upvotes: 2

Related Questions