Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play video inline with WkWebView

When you use an iphone and play video in a webview, this video is open in the native player in fullscreen.

We have tried UIWebView and WKWebView with "allowsInlineMediaPlayback" property to true. But the video in the web content launch in fullscreen with an iphone iOS 10.2. Have you and idea what i can do ?

let webConfiguration = WKWebViewConfiguration() // Fix Fullscreen mode for video and autoplay webConfiguration.preferences.javaScriptEnabled = true webConfiguration.mediaPlaybackRequiresUserAction = false webConfiguration.allowsInlineMediaPlayback = true  webView = WKWebView(frame: CGRect(x: 0, y: 0, width:self.backgroundView.frame.width, height:self.backgroundView.frame.height), configuration: webConfiguration) 

Env : Xcode 8, swift 3

like image 528
UBLearn Avatar asked May 10 '17 07:05

UBLearn


People also ask

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

What is the difference between UIWebView and WKWebView?

Difference Between UIWebview and WKWebViewUIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

How do I use WKWebView?

Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.

Is WKWebView secure?

The WKWebView is a modern API applying all the modern web security mechanisms, it's still maintained by Apple and gets updates. The good thing about WKWebView is that it does out-of-process rendering, so if the attackers find a memory corruption vulnerability in it, your application's process is still isolated.


2 Answers

There is no problem for your code,but you need one more step, the video URL you use should always with a parameter playsinline=1.

//step1 if let videoURL:URL = URL(string: "https://somevideo.mp4?playsinline=1") //step2 webConfiguration.allowsInlineMediaPlayback = true 

then you can do the left things.

like image 190
onewh Avatar answered Sep 24 '22 02:09

onewh


Here is a solution as you want, i make player programatically and change some code, to play video inline.

var myPlayer: WKWebView!      override func viewDidLoad() {         super.viewDidLoad()          let webConfiguration = WKWebViewConfiguration()         webConfiguration.allowsInlineMediaPlayback = true         webConfiguration.mediaTypesRequiringUserActionForPlayback = []          myPlayer = WKWebView(frame: CGRect(x: 0, y: 0, width: 375, height: 300), configuration: webConfiguration)         self.view.addSubview(myPlayer)          if let videoURL:URL = URL(string: "https://www.youtube.com/embed/9n1e1N0Sa9k?playsinline=1") {              let request:URLRequest = URLRequest(url: videoURL)              myPlayer.load(request)         }          //OR to show player control also, use this         /*if let videoURL:URL = URL(string: "https://www.youtube.com/embed/9n1e1N0Sa9k?playsinline=1&controls:1") {              let request:URLRequest = URLRequest(url: videoURL)              myPlayer.load(request)         }*/     } 
like image 37
Hardik Thakkar Avatar answered Sep 26 '22 02:09

Hardik Thakkar