Énn*_*an 5 io uiwebview webview swift
我想在网络视图中检测视频全屏进入和退出,并在每种情况下执行一些代码,我需要捕获和处理哪些事件,观察者是否能做到这一点,向用户显示某些选项对我来说至关重要并修改接口。
这是我用于网络视图的代码的一部分
请帮助拯救我的日子
func loadWebSite() {
let theConfiguration:WKWebViewConfiguration? = WKWebViewConfiguration()
let thisPref:WKPreferences = WKPreferences()
thisPref.javaScriptCanOpenWindowsAutomatically = true;
thisPref.javaScriptEnabled = true
theConfiguration!.preferences = thisPref;
self.wkWebView = WKWebView(frame: self.getFrame(), configuration: theConfiguration!)
self.wkWebView?.navigationDelegate = self
self.wkWebView?.uiDelegate = self
self.wkWebView?.bridge.printScriptMessageAutomatically = true
self.wkWebView?.addObserver(self, forKeyPath: "loading", options: .new, context: nil)
self.wkWebView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
self.wkWebView?.addObserver(self, forKeyPath: "VideoExitFullScreen", options: .new, context: nil)
self.wkWebView?.addObserver(self, forKeyPath: "VideoEnterFullScreen", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "loading") {
self.backButton?.isEnabled = self.wkWebView!.canGoBack
self.forwardButton?.isEnabled = self.wkWebView!.canGoForward
} else if (keyPath == "estimatedProgress") {
let estimatedProgress = Float(self.wkWebView!.estimatedProgress)
if estimatedProgress > 0.90 {
}
}else if (keyPath == "VideoExitFullScreen") {
// DO CODE HERE
} else if (keyPath == "VideoEnterFullScreen") {
// DO CODE HERE
}
}
Run Code Online (Sandbox Code Playgroud)
迅速
我想要的是检测视频何时退出全屏并再次播放。
NotificationCenter.default.addObserver(self, selector: #selector(onDidEnterFullscreen(_:)), name: UIWindow.didBecomeVisibleNotification, object: webview)
NotificationCenter.default.addObserver(self, selector: #selector(onDidLeaveFullscreen(_:)), name: UIWindow.didBecomeHiddenNotification, object: webview)
@objc func onDidEnterFullscreen(_ notification: Notification) {
print("video is now playing in fullscreen")
}
@objc func onDidLeaveFullscreen(_ notification: Notification) {
print("video has stopped playing in fullscreen")
// This line plays the video again when exiting full screen
webview?.evaluateJavaScript("var videos = document.getElementsByTagName(\"video\"); for (var i = 0; i < videos.length; i++) { videos.item(i).play(); };")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
585 次 |
| 最近记录: |