Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

wkwebview

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.

like image 551
Henrik N Avatar asked Feb 04 '26 06:02

Henrik N


2 Answers

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

like image 126
Jason B Avatar answered Feb 05 '26 22:02

Jason B


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!

like image 27
Peer Mohamed Thabib Avatar answered Feb 05 '26 20:02

Peer Mohamed Thabib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!