JDev
JDev

Reputation: 5558

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?

Upvotes: 5

Views: 11171

Answers (1)

jikigi
jikigi

Reputation: 176

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)

Upvotes: 15

Related Questions