【问题标题】:WKWebView swiftUI crash on second attemptWKWebView swiftUI 在第二次尝试时崩溃
【发布时间】:2020-11-29 19:36:50
【问题描述】:

我在 SwiftUI 视图内的 WKWebView 中播放 youtube 视频(嵌入 url)时遇到问题。

这是我的 SwiftUI WebView 代码

import SwiftUI
import UIKit
import WebKit

struct WebView: UIViewRepresentable {
    
    let url: String

    func makeUIView(context: Context) -> WKWebView {
        let webViewConfig = WKWebViewConfiguration()
        webViewConfig.mediaTypesRequiringUserActionForPlayback = .all
        webViewConfig.allowsInlineMediaPlayback = true
        return WKWebView(frame: .zero, configuration: webViewConfig)
    }

    func updateUIView(_ view: WKWebView, context: UIViewRepresentableContext<WebView>) {
        view.load(request)
    }
}

我像Webview(url: "YOUTUBEEMBEDVIDEOURLHERE")这样使用这个WebView,重点如下,我第一次进入屏幕并点击播放按钮,url呈现一切都很顺利,此时,我返回(使用NavigationView ) 并再次进入同一屏幕并再次单击播放视频,然后我就崩溃了。

崩溃信息


Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=3 "Required target entitlement is missing" UserInfo={RBSAssertionAttribute=<RBSDomainAttribute| domain:"com.apple.webkit" name:"Background" sourceEnvironment:"(null)">, NSLocalizedFailureReason=Required target entitlement is missing}>

[ProcessSuspension] 0x1479e9930 - ProcessAssertion: Failed to acquire RBS Background assertion 'WebProcess Background Assertion' for process with PID 60999, error: Error Domain=RBSAssertionErrorDomain Code=3 "Required target entitlement is missing" UserInfo={RBSAssertionAttribute=<RBSDomainAttribute| domain:"com.apple.webkit" name:"Background" sourceEnvironment:"(null)">, NSLocalizedFailureReason=Required target entitlement is missing}

Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}>

ProcessAssertion: Failed to acquire RBS Background assertion 'WebProcess Background Assertion' for process with PID 60999, error: Error Domain=RBSAssertionErrorDomain Code=2 "Specified target process does not exist" UserInfo={NSLocalizedFailureReason=Specified target process does not exist}

_userSettingsForUser : (null)

 _WebFilterIsActive returning: NO

我尝试启用后台权限,但没有成功

可能与这个错误https://developer.apple.com/forums/thread/121822有关?

【问题讨论】:

  • 我在这里遇到了完全相同的问题 .. 使用带电容器的 Ionic 5 .. 仅适用于 iOS 14。这与 https://github.com/ionic-team/ionic-framework/issues/21613 有关。你找到解决办法了吗?
  • 不,我没有,我终于用另一个库来播放视频,它工作,尝试找到一个替代库来做,我只能说=(,保重!
  • 我只有在 iOS 上有同样的问题。 14、你有没有找到解决办法?

标签: ios background swiftui wkwebview


【解决方案1】:

您可以尝试使用以下实现并查看它是否有效,使用执行以下操作

WebView(request: URLRequest(url: URL(string: "https://www.youtube.com/watch?v=HTnwBT0ECAU")!)    

import SwiftUI
import WebKit
struct WebView: UIViewRepresentable {

let request: URLRequest

    func makeUIView(context: Context) -> WKWebView {
        let wkWebView = WKWebView()
        wkWebView.navigationDelegate = context.coordinator
        return wkWebView
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {
        uiView.load(request)
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    class Coordinator: NSObject, WKNavigationDelegate {
        var webView: WebView
        init(_ webView: WebView) {
            self.webView = webView
        }
    }
}

【讨论】:

    【解决方案2】:

    确保WKWebView 具有父视图。如果没有 - 尝试创建一个空的 UIView 并将 WKWebView 添加为子视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多