通知内容扩展中的 WKWebView 在手机锁定时从不加载

Posted

技术标签:

【中文标题】通知内容扩展中的 WKWebView 在手机锁定时从不加载【英文标题】:WKWebView in Notification Content Extension never loads while phone is locked 【发布时间】:2021-09-30 17:49:02 【问题描述】:

NCE(通知内容扩展)中,我使用WKWebView,它加载本地和远程内容

但是,当手机被锁定时,方法

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)

永远不会被调用,而且

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)

不会因任何错误而被调用

webview 在加载时卡住了...但手机解锁后一切正常

这是一个错误,还是有意为之?如何避免这种情况,或检测此问题

在手机锁定时激活 NCE 期间的控制台输出

2021-10-01 12:12:25.737559+0200 Minuta-NCE[4223:493144] [Process] 0x1022ce100 - [PID=4253] WebProcessProxy::didClose: (web process 4253 crash)
2021-10-01 12:12:25.737619+0200 Minuta-NCE[4223:493144] [Process] 0x1022ce100 - [PID=4253] WebProcessProxy::processDidTerminateOrFailedToLaunch: reason=3
2021-10-01 12:12:25.737726+0200 Minuta-NCE[4223:493144] [Process] 0x101824c18 - [pageProxyID=69, webPageID=70, PID=4253] WebPageProxy::processDidTerminate: (pid 4253), reason 3
2021-10-01 12:12:25.739596+0200 Minuta-NCE[4223:493144] [Loading] 0x101824c18 - [pageProxyID=69, webPageID=70, PID=4253] WebPageProxy::dispatchProcessDidTerminate: reason=3
2021-10-01 12:12:25.739746+0200 Minuta-NCE[4223:493144] [Process] 0x101824c18 - [pageProxyID=69, webPageID=70, PID=4253] WebPageProxy::tryReloadAfterProcessTermination: process crashed and the client did not handle it, not reloading the page because we reached the maximum number of attempts
2021-10-01 12:12:25.740206+0200 Minuta-NCE[4223:494960] [assertion] Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo=NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit>
2021-10-01 12:12:25.741077+0200 Minuta-NCE[4223:494960] [ProcessSuspension] 0x1022f9980 - ProcessAssertion: Failed to acquire RBS assertion 'ConnectionTerminationWatchdog' for process with PID=4253, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo=NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit
2021-10-01 12:12:25.742912+0200 Minuta-NCE[4223:494960] [assertion] Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo=NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit>
2021-10-01 12:12:25.742953+0200 Minuta-NCE[4223:494960] [ProcessSuspension] 0x1022f99e0 - ProcessAssertion: Failed to acquire RBS assertion 'WebProcess Background Assertion' for process with PID=4253, error: Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit" UserInfo=NSLocalizedFailureReason=target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit
2021-10-01 12:12:25.746129+0200 Minuta-NCE[4223:494960] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo=NSLocalizedFailureReason=Specified target process does not exist>
2021-10-01 12:12:25.746167+0200 Minuta-NCE[4223:494960] [ProcessSuspension] 0x1022f9aa0 - ProcessAssertion: Failed to acquire RBS assertion 'WebProcess Background Assertion' for process with PID=4253, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo=NSLocalizedFailureReason=Specified target process does not exist
2021-10-01 12:12:25.746444+0200 Minuta-NCE[4223:494960] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo=NSLocalizedFailureReason=Specified target process does not exist>
2021-10-01 12:12:25.747201+0200 Minuta-NCE[4223:494960] [ProcessSuspension] 0x1022f9b00 - ProcessAssertion: Failed to acquire RBS assertion 'WebProcess Suspended Assertion' for process with PID=4253, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo=NSLocalizedFailureReason=Specified target process does not exist

但如前所述,遗憾的是代码中没有返回错误

【问题讨论】:

【参考方案1】:

目前我只通过在阈值计时器后显示的用户消息来解决此问题

self.webView.loadFileURL(fileURL, allowingReadAccessTo: baseURL)
    
DispatchQueue.main.asyncAfter(deadline: .now() + 2)  [weak self] in
    
    guard let self = self else return
    if (!self.webLoaded && !self.webError) 
        self.infoLabel.text = "Device must be unlocked!"
    
    

【讨论】:

尝试在共享扩展中加载 URL 内容时,我收到此错误 Error Domain=RBSServiceErrorDomain Code=1 "target is not running or doesn't have entitlement com.apple.runningboard.assertions.webkit"。内容已加载,但我在控制台中看到了这一点。扩展是否需要授权才能运行 WebKit? (我正在使用 LPLinkView,它可能使用 WKWebView 来获取元数据) 我没有找到任何可以使其正常工作的权利......至少没有记录 谢谢,有时间可以看看***.com/questions/70118893/…

以上是关于通知内容扩展中的 WKWebView 在手机锁定时从不加载的主要内容,如果未能解决你的问题,请参考以下文章

swift中的本地通知警报声

iphone锁定时带有声音和振动的通知

锁定用户与WKWebView的交互

从后台服务打开应用程序屏幕并锁定手机屏幕时冻结

实际显示视图时的 WKWebView 通知

如何检测 WKWebView 正在关闭,来自 javascript