iosswiftavplayerwkwebviewwkwebviewconfiguration

How to disable auto-start AVPlayer in wkwebview video


I want to set alert like Safari, when user click on a video url then it will ask for to play or cancel.

currently when I click on video url(a url inside of a loaded page) it directly play the video in AVPlayer.

enter image description here

How do I implement it without auto-start?


Solution

  • This answer works in my case.

    on click of downloadable link where videos will play I use below delegate of wkwebview:

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            print("downloadable link")
    
            // add alert here
    
            decisionHandler(WKNavigationActionPolicy.cancel)
        }else{
            decisionHandler(WKNavigationActionPolicy.allow)
    }
    

    here(on place of add alert here) you can add the alert same like Safari and use as per your requirement.