如何终止 WKWebView 来触发 webViewWebContentProcessDidTerminate?

问题描述 投票:0回答:2

我想验证我的应用程序如何处理

webViewWebContentProcessDidTerminate

我可以直接调用我的代表的

webViewWebContentProcessDidTerminate
,但这并不能让我完全相信它的行为与现实生活中一样。

如何终止 WKWebView,从而触发此回调?

ios wkwebview
2个回答
1
投票

如果您可能知道,当 com.apple.WebKit.WebContent 将被操作系统终止时,将会调用

webViewWebContentProcessDidTerminate

在大多数情况下,这是由内存不足问题引起的。 就我个人而言,我遇到过 WkWebView 使用大量内存但不是在非关键级别终止,同时另一个视图已放置在 Webview 上(这意味着 com.apple.WebKit.WebContent 进入后台)。

如果您有兴趣如何实际触发调用此方法,请尝试通过 OOM 错误使其崩溃:

  1. 加载 HTML 页面,正文中包含下一个 JS 字符串:

    const OOMString = Array(1024 * 1024 * 1000).fill('A').join(''));

  2. 如果您打算从本机端(ObjectiveC/Swift)执行此操作,这里是另一个示例:

    [wkWebView evaluateJavaScript:@"window.OOMString = Array(1024 * 1024 * 1000).fill('A').join(''));" completionHandler:nil];

您需要考虑到操作系统决定终止您的进程时将调用

webViewWebContentProcessDidTerminate
。对于不同的设备将允许占用不同大小的内存。 我强烈不建议您以这种方式加载内存,因为它会影响您的用户体验或客户体验。这里最好的方法是尽可能不允许系统调用此方法。


0
投票

我有同样的问题,我想触发 webViewWebContentProcessDidTerminate。这是我在 github issues 中找到的内容,它对我有用。

# 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')
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.