webViewWebContentProcessDidTerminate 是否曾经调用过在后台运行的应用程序?

发布于 2025-01-11 19:11:26 字数 579 浏览 1 评论 0原文

如果 WKWebView 在应用程序在前台运行时终止,webViewWebContentProcessDidTerminate将在其委托上调用。

但如果应用程序在后台运行会发生什么?

还会立即收到回调吗?当应用程序稍后置于前台时它会收到它吗?它根本不运行吗?

我目前不知道如何触发终止——否则我就尝试一下。

我问这个问题是因为我想让它重新加载已终止的 Web 视图,以避免我的应用程序中出现空白屏幕,但我不确定这是否真的适用于在后台运行的应用程序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2025-01-18 19:11:26

如果您正在运行模拟器,则可以从命令行终止 Web 内容进程以触发 webViewWebContentProcessDidTerminate 调用:

# 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')

此处要点:https://gist.github.com/jasonbekolay/dad7c446ae1b02f174dc3eb3a5ea70ee

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

紙鸢 2025-01-18 19:11:26

将我的答案添加到所提出的问题中,以便任何人都可以从中受益。

但是如果应用程序在后台运行会发生什么情况?
还会立即收到回调吗?当应用程序稍后置于前台时它会收到它吗?它根本不运行吗?

进行了快速测试,答案是“是”,即使应用程序处于后台也立即收到回调

我目前不知道如何触发终止 - 否则,我会尝试一下。

这个问题之前的答案是正确的,我想在此基础上添加我的发现。

如果你是像我一样讨厌的人这里的命令行是基于 GUI 的解决方案。

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

活动监视器示例

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)

第 3 步和第 4 步屏幕参考

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

处理选择< /a>

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

停止进程示例

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

webViewWebContentProcessDidTerminate

如果您像我一样想要在真实设备上测试它,不幸的是,活动监视器在 iOS 设备上不可用(据我所知,如果有任何其他的,请添加您的评论选项)。

不用担心,你可以按照我的方法在真机上进行模拟。

尝试查找使用基于 WebView 的内容呈现的应用程序。

例如,Gmail 应用程序。打开收件箱中的每封电子邮件(有用提示:左右滑动可在上一封和下一封电子邮件之间导航,以使过程更快)并使用它一段时间(最好的选择是内容- 大量新闻通讯)

另一种选择是使用设备上的 Safari 浏览器。使用一段时间你就能模拟了。

注 1: 确保您没有在后台运行多个应用程序,否则操作系统将在后台状态终止您的应用程序。当您重新打开应用程序时,它将重新启动,您的努力将被浪费。

注2:尝试在低端设备上执行此操作,因为它更容易模拟。

希望这个答案对您有所帮助,调试愉快!

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!

汹涌人海 2025-01-18 19:11:26

对我来说,仅当我的应用程序位于后台并且 iOS 缺乏用于终止我的应用程序中的 WKWebView 的其他应用程序的内存时,WKWebView 才会终止。如果您再次打开应用程序,WKWebView 将被破坏并调用 webViewWebContentProcessDidTerminate

尝试复制为@joemasilotti指出

  1. 打开您的应用程序a WKWebView active
  2. 将您的应用程序移动到后台 - 不要终止它!
  3. 打开许多其他应用程序以使用内存,或者玩一些图形密集型游戏几秒钟。
  4. 尝试再次打开您的应用程序,您应该会看到空白/灰色屏幕。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文