Search code examples
iosswiftuiwebviewwkwebview

How to enable embedded YouTube videos in WKWebView?


How would one go about playing an embedded video like YouTube in their WKWebView? When launching an embedded YouTube video from a UIWebView, it plays just fine – whereas in the WKWebView it automatically plays in fullscreen. Am I missing something?


Solution

  • I had a similar issue as you. What did it for me was to change the allowsInlineMediaPlayback property of WKWebViewConfiguration to true and setting the playsinline property to 1 in the YouTube player.

    webViewConfiguration.allowsInlineMediaPlayback = true
    wkWebView = WKWebView(frame: self.view.frame, configuration: webViewConfiguration)
    let myURL = URL(string: "https://www.youtube.com/embed/BY-aB72nONA?playsinline=1")
    var youtubeRequest = URLRequest(url: myURL!)
    wkWebView.load(youtubeRequest)