Why Wkwebview Doesn't Display Videos? - Swift 3

Why WKWebView doesn't display videos? - Swift 3

I think the video is playing behind your WKWebview, could you please debug the view hierarchy and post it? Xcode->Menu->Debug->View Debugging->Capture View Hierarchy when playing the video.

I've tried with a new swift3 project and with your code, there is no problem, here is the view hierarchy:

Sample Image

You can see that from left pannel, the AVPlayerView is in another UIWindow, different from WKWebView,So I guess the UIWindow which contains the WKWebView in your project has a higher windowLevel so it shows above the UIWindow which contains AVPlayer.

And by making the default UIWindow a higher WindowLevel(UIWindowLevelAlert),I reproduced what you've seen in your project.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window?.windowLevel = UIWindowLevelAlert
return true
}

WKWebView video does not play inline

From looking at some other issues people have reported and the detail in the Apple documentation, I think the issue is that WKWebViewConfiguration has to be set before the WKWebView is created.

From the apple documentation:

Using the WKWebViewConfiguration class, you can determine how soon a webpage is rendered, how media playback is handled, the granularity of items that the user can select, and many other options.

WKWebViewConfiguration is only used when a web view is first initialized. You cannot use this class to change the web view's configuration after it has been created.

This aligns with the example that apple provide for using a WKWebView (https://developer.apple.com/documentation/webkit/wkwebview) where you cans see the WKWebViewConfiguration is passed to the WKWebView set up. I've added in the playsinline as an example for your case.

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {

var webView: WKWebView!

override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true //** Added as an example for your case
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()

let myURL = URL(string:"https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}

It would seem to make more sense if the property could not be set subsequently to avoid this confusion, but it appears from the documentation this is how it works.

WKWebView not loading webpage - renders blank screen in Swift

I have found the solution. The problem was being caused by AVG AntiVirus's webshield. For some reason AVG webshield treats all network communication from the simulator as fraudulent.
The following screenshot shows the safari app running on simulator. It says that www.apple.com is not safe or any other website.

Safari screenshot

The following screenshot is from system.log showing errors with webkit.

System log

You can replicate this problem by installing AVG antivirus and turning on the webshield. WKWebview in your App(On the simulator) wouldn't load anything.

I don't understand why it wasn't working on an actual device tho. It could have been another problem with the device. I also deleted the derived data folder, the actual app and had restarted the device.

Thank you everybody for the answers and help.

wkwebview won't display a youtube video in an iframe

Go to Projects info.plist file
Added a Key called NSAppTransportSecurity as a Dictionary.
Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES. It works.Sample Image

iOS10: Auto play not working on WKWebView (requiresUserActionForMediaPlayback = false)

use this for iOS10:

var mediaTypesRequiringUserActionForPlayback: WKAudiovisualMediaTypes { get set }



Related Topics



Leave a reply



Submit