WKWebView swiftUI 在第二次尝试时崩溃

jpl*_*850 8 background ios wkwebview swiftui

我在 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

我像这样使用这个WebView Webview(url: "YOUTUBEEMBEDVIDEOURLHERE"),重点如下,第一次进入屏幕并点击播放按钮,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

Run Code Online (Sandbox Code Playgroud)

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

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

Har*_*ini 2

您可以尝试使用以下实现并查看它的工作原理吗,要使用执行以下操作

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)