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:
After Tapping:
I also tried another web page:
Before tapping:
After Tapping (Note that the status bar is hidden now):
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With