检测WebViewvideo在ios8上变成全屏时的情况
我有一个应用程序,用户可以从UIWebview打开video,包括Youtube的。 在iOS7中,当我开始播放时,或者当它变成全屏时,我得到了一个通知,这对于我向用户显示某些选项并修改界面至关重要。
我曾经使用这个:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoExitFullScreen:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoEnterFullScreen:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
不过,自iOS8以来,我无法做到这一点。 这就像通知不再从UIWebviewvideo触发。 但是,正如我testing的那样,它仍然是从正常的video,非Web视图触发的。
任何想法都改变了?
这是我find的工作
[[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];
对于Swift和iOS 9:
NSNotificationCenter.defaultCenter().addObserverForName( UIWindowDidResignKeyNotification, object: self.view.window, queue: nil ) { notification in print("Video is now fullscreen") } NSNotificationCenter.defaultCenter().addObserverForName( UIWindowDidBecomeKeyNotification, object: self.view.window, queue: nil ) { notification in print("Video stopped") }
为了迅速:
NotificationCenter.default.addObserver(self, selector: #selector(xxx), name: NSNotification.Name.MPMoviePlayerDidExitFullscreen, object: nil)
@ NorthBlast的答案适用于检测任何UIWindow
出现在UIViewController
顶部的UIWebView
。 不幸的是,很难过滤什么样的UIWindow
(因为,你不能真正知道它是一个video或其他types的窗口)。
有三种我喜欢过滤的特殊情况,其中你确定他们不是video播放器窗口,那些是:
1) _UIAlertControllerShimPresenterWindow
,这是一种使用警报(如UIAlertView
)时出现的窗口。
2) UITextEffectsWindow
,当呈现特殊的iOS窗口(如共享窗口, UIActivityViewController
)时出现。
3)显示键盘时出现的UIRemoteKeyboardWindow
(出于某种原因,这个类只在使用Swift时才出现,但在Objective-C上它没有…不知道为什么)。
所以订阅通知,我使用(就像@NorthBlast说):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeActive:) name:UIWindowDidBecomeVisibleNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeHidden:) name:UIWindowDidBecomeHiddenNotification object:nil];
然后执行:
- (void)windowDidBecomeActive:(NSNotification *)notification { if ( [self isVideoPlayerWindow:notification.object] ) { // Do what's needed if it is a video // For example, on a live streaming radio app, I would stop the audio if a video is started } } - (void)windowDidBecomeHidden:(NSNotification *)notification { if ( [self isVideoPlayerWindow:notification.object] ) { // Do what's needed if it is a video } } - (BOOL)isVideoPlayerWindow:(id)notificationObject { /* Define non video classes here, add more if you need it */ static NSArray *nonVideoClasses = @[ @"_UIAlertControllerShimPresenterWindow", @"UITextEffectsWindow", @"UIRemoteKeyboardWindow" ]; BOOL isVideo = YES; for ( NSString *testClass in nonVideoClasses ) { isVideo = isVideo && ! [notificationObject isKindOfClass:NSClassFromString(testClass)]; } return isVideo; }