WKWebView swiftUI crash on second attempt

Viewed 6210

I have a problem playing a youtube video (embed url) within a WKWebView inside a SwiftUI view.

This is my SwiftUI WebView code

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)
    }
}

I use this WebView like this Webview(url: "YOUTUBEEMBEDVIDEOURLHERE"), the point is the following, first time I get into the screen and click on play button that the url renders everything goes very nice, at this point, I go back (using NavigationView) and get into the same screen again and click on play the video again and then I get the crash.

Crash info


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

I tried enabling background permissions and it didn't work

Could be related with this bug https://developer.apple.com/forums/thread/121822 ?

2 Answers

Can you try using the following implementation and see it works, to use do the following

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
        }
    }
}

Ensure WKWebView has a parent view. If it doesn't - try to create an empty UIView and add WKWebView as a subview.

Related