swiftuicamerawkwebview

WKWebView in SwiftUI not exposing UserFacing Camera


My WKWebView is not exposing the user facing camera even though I specifically request it. It always uses the rear camera.

Anything special I need to consider?

            if url != nil {
                WebViewWrapper(url: $url, tripleTapAction: {
                    showAlert = true
                })
                .modifier(InteractionModifier(updateLastActivityDate: updateLastActivityDate))
                .ignoresSafeArea(.all, edges: .bottom)
            } else {
                VStack {
                    Text("No URL provided")
                        .foregroundColor(.gray)
                    
                    Button("Add URL") {
                        showAlert = true
                    }
                }
            }
        }

Config of WKWebView (DataStore has no impact I already tried)

let configuration = WKWebViewConfiguration()
        configuration.websiteDataStore = .nonPersistent()
        
        let webView = WKWebView(frame: .zero, configuration: configuration)
        webView.navigationDelegate = context.coordinator
        webView.isUserInteractionEnabled = true // Ensure user interaction is enabled

Javascript

const constraints = {
            video: {
                facingMode: { exact: 'user' } // Use exact to enforce the front camera
            }
        };

Solution

  • The issue was related to the fact that in JS we first need to call

    navigator.mediaDevices.getUserMedia(constraints);

    to get the proper available devices.

    Meaning first call

    navigator.mediaDevices.getUserMedia(constraints);

    and after that you will have e.g. a user video available if the device supports it