检测webview视频何时在ios8上全屏显示

eip*_*rol 14 youtube video mpmovieplayercontroller uiwebview ios

我有一个应用程序,用户可以从UIWebview打开视频,包括Youtube.在iOS7中,我能够在开始播放或者全屏时收到通知,这对我向用户显示某些选项和修改界面至关重要.

我以前用这个:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoExitFullScreen:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoEnterFullScreen:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
Run Code Online (Sandbox Code Playgroud)

但是,自iOS8以来,我无法实现这一点.这就像UIWebview视频不再触发通知一样.但是,正如我测试的那样,它仍然是从普通视频,非Webview触发的.

什么改变了?

val*_*u17 24

这是我为此找到的工作..

[[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];
Run Code Online (Sandbox Code Playgroud)

  • 我发现当视频开始播放全屏时会调用**UIWindowDidBecomeVisibleNotification**.当视频消失时,会调用**UIWindowDidBecomeHiddenNotification**. (4认同)

Tie*_*eme 5

对于Swift和iOS 9:

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")
}
Run Code Online (Sandbox Code Playgroud)


小智 5

Swift 4.2、iOS 12.1 和 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")
}
Run Code Online (Sandbox Code Playgroud)