Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why WKWebView doesn't display videos? - Swift 3

I successfully added a WKWebView programmatically In a viewController (in viewDidLoad()). When loading a url that contains a video it appears fine, but when I try to tap on it (playing it), I cannot see it, but the audio works fine.

The weird thing is I created a new project just to make sure it should works fine and it did, I copied the same exact code the webView displayed the video as it should.

It was working fine before converting to Swift 3.

This is how it looks when tapping on the video:

Before tapping:

enter image description here

After Tapping:

enter image description here

I also tried another web page:

Before tapping:

enter image description here

After Tapping (Note that the status bar is hidden now):

enter image description here

Simply, this is the code:

override func viewDidLoad() {
    super.viewDidLoad()

    let web = WKWebView(frame: view.frame)
    let urlRequest = URLRequest(url: URL(string: "http://www.w3schools.com/html/html5_video.asp")!)       
    web.frame = view.frame
    web.load(urlRequest)

    view.addSubview(web)
 }

I tried to check many cases without any output. What am I missing?

Thanks in advance.

like image 277
A.Munzer Avatar asked Jan 05 '23 05:01

A.Munzer


1 Answers

When initialising the webview, you need to pass in two configuration properties. Example:

let webConfiguration = WKWebViewConfiguration()
    webConfiguration.allowsInlineMediaPlayback = true
    webConfiguration.mediaTypesRequiringUserActionForPlayback = []

    webView = WKWebView(frame: .zero, configuration: webConfiguration)

You also need to allow arbitrary loads in info.plist:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
</dict>
like image 175
Per Quested Aronsson Avatar answered Jan 11 '23 15:01

Per Quested Aronsson