Detect when a webview video becomes fullscreen on ios8
This is the work around I found for this..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(VideoExitFullScreen:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(VideoEnterFullScreen:)
name:UIWindowDidBecomeHiddenNotification
object:self.view.window];
update for Swift 4.2, iOS 12.1 and WKWebView:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// listen for videos playing in fullscreen
NotificationCenter.default.addObserver(self, selector: #selector(onDidEnterFullscreen(_:)), name: UIWindow.didBecomeVisibleNotification, object: view.window)
// listen for videos stopping to play in fullscreen
NotificationCenter.default.addObserver(self, selector: #selector(onDidLeaveFullscreen(_:)), name: UIWindow.didBecomeHiddenNotification, object: view.window)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// remove video listeners
NotificationCenter.default.removeObserver(self, name: UIWindow.didBecomeVisibleNotification, object: view.window)
NotificationCenter.default.removeObserver(self, name: UIWindow.didBecomeHiddenNotification, object: view.window)
}
@objc func onDidEnterFullscreen(_ notification: Notification) {
print("video is now playing in fullscreen")
}
@objc func onDidLeaveFullscreen(_ notification: Notification) {
print("video has stopped playing in fullscreen")
}
Swift 5.1:
NotificationCenter.default.addObserver(
forName: UIWindow.didResignKeyNotification,
object: self.view.window,
queue: nil
) { notification in
print("Video is now fullscreen")
}
NotificationCenter.default.addObserver(
forName: UIWindow.didBecomeKeyNotification,
object: self.view.window,
queue: nil
) { notification in
print("Video stopped")
}