How to handle website requesting fullscreen in visionOS?

I am working on a native visionOS app.

I'm running into a problem when websites request fullscreen.

Sample code:

import SwiftUI
import WebKit

struct WebViewWrapper: UIViewRepresentable {
    let url: String
    
    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = false
        webConfiguration.mediaPlaybackRequiresUserAction = false

        
        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.navigationDelegate = context.coordinator
        webView.uiDelegate = context.coordinator
        
        return webView
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
        if let url = URL(string: url) {
            let request = URLRequest(url: url)
            uiView.load(request)
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, WKNavigationDelegate, WKUIDelegate {
        var parent: WebViewWrapper
        
        init(_ parent: WebViewWrapper) {
            self.parent = parent
        }
        
        func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
            if navigationAction.targetFrame == nil {
                webView.load(navigationAction.request)
            }
            return nil
        }
    }
}

struct ContentView: View {
    var body: some View {
        GeometryReader { geo in
            VStack {
                WebViewWrapper(url: "https://youtube.com")
                    .clipShape(RoundedRectangle(cornerSize: CGSize(width: geo.size.width/40, height: geo.size.height/40)))
                    .padding()

            }
        }
    }
}

#Preview(windowStyle: .automatic) {
    ContentView()
}

The error I'm getting: -[AVPlayerViewController enterFullScreenAnimated:completionHandler:] failed with error Invalid call of -[AVPlayerViewController (0x153087600) _transitionToFullScreenAnimated:interactive:completionHandler:]!

Any help would be greatly appreciated.

I am also trying to get full-screen working with WKWebView, it it appears to go full screen without an error, but always appears behind glass overlay? Very frustrating, I have no idea why. (See image)

Here is my code:

    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.preferences.isElementFullscreenEnabled = true
        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
        webView.configuration.allowsInlineMediaPlayback = true
        webView.load(URLRequest(url: URL(string: "https://www.youtube.com/watch?v=7q1eDpuBFnI")!))
        return webView
    }

I noticed you don't have:

 webConfiguration.preferences.isElementFullscreenEnabled = true

But maybe that is intended for your coordinator approach. Were you able to figure it out?

Was anyone able to figure out a workaround to make fullscreen video work inside a webview in VisionOS? I as well see all videos open behind the glass.

+1 I would love to find a solution for that bug. Still there on visionOS 1.2 beta 5...

Also happening to me on visionOS 1.2. Happening on different video services (Youtube, Vimeo, etc.) Interestingly, does not happen on https://developer.apple.com/wwdc24/sessions/

How to handle website requesting fullscreen in visionOS?
 
 
Q