Home > OS >  How can disable picture in picture on video was playing in webview?
How can disable picture in picture on video was playing in webview?

Time:01-02

enter image description hereHow can disable picture in picture on video was playing in webview ? When you go to the video via the web, it opens the special autoplay in iOS Is it possible to hide the button picture in picture?

self.activityIndicator.stopAnimating()
self.activityIndicator.isHidden = true
guard let vid =  videosID else {return}
let weburl = NSURL(string: "https://mosesplayer.azurewebsites.net/Electronplayer/Viewer?vid=\(vid)&source=Mobile")
let request = NSMutableURLRequest(url: weburl! as URL)
print(xapt)
request.setValue(  xapt , forHTTPHeaderField:"x-apt")
webView.configuration.allowsPictureInPictureMediaPlayback = false
self.webView.load( request as URLRequest)

enter image description here

CodePudding user response:

I think it's not possible to remove the button from the WebView itself. You rather have to add the disablePictureInPicture attribute to the HTML video tag.

<video controls disablePictureInPicture controlsList="nodownload">
  <source src="https://www.w3schools.com/html/mov_bbb.ogg" type="video/mp4">
  <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/ogg">
</video>

You can change the HTML by injecting JavaScript into the WebView.

    class MainVC: UIViewController {
        
        @IBOutlet weak var webView: WKWebView!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // 1
            webView.load(URLRequest(url: URL(string: "URL")!))
            
            injectToPage()
        }
        
        // 2
        // MARK: - Reading contents of files
        private func readFileBy(name: String, type: String) -> String {
            guard let path = Bundle.main.path(forResource: name, ofType: type) else {
                return "Failed to find path"
            }
            
            do {
                return try String(contentsOfFile: path, encoding: .utf8)
            } catch {
                return "Unkown Error"
            }
        }
        
        // 3
        // MARK: - Inject to web page
        func injectToPage() {

   let jsFile = readFileBy(name: "script", type: "js")
   let jsScript = WKUserScript(source: jsFile, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
         webView.configuration.userContentController.addUserScript(jsScript)
        }
    
    }

script.js

let videos = document.getElementsByTagName('video');

for(let video of videos) {
    video.setAttribute('disablePictureInPicture', '');
}
  • Related