如何禁用在 webview 中播放的视频中的画中画?

How can disable picture in picture on video was playing in webview?

如何禁用在 webview 中播放的视频中的画中画? 当您通过网络访问视频时,它会在 iOS 中打开特殊的自动播放 是否可以隐藏图片中的按钮图片?

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)

我认为无法从 WebView 本身删除按钮。您宁愿必须将 disablePictureInPicture 属性添加到 HTML 视频标签。

<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>

您可以通过 injecting JavaScript 将 HTML 更改为 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', '');
}