Detect when a webview video becomes fullscreen on ios8

I have an app where users can open videos from UIWebview, including Youtube ones.
In iOS7, I was able to get a notification when it started playing, or when it became full screen, which is vital for me to show certain options to the user and modify the interface.

I used to use this:

  • iOS app crash after UITextField text entry, pop, then push on my navController (NSISObjectiveLinearExpression coefficientForVariable)
  • UIAppearance setTranslucent error: Illegal property type, c for appearance setter, _installAppearanceSwizzleForSetter
  • Separate Settings in Universal iOS App?
  • Displaying download progress in reusable cells
  • Is it possible to set UIView border properties from interface builder?
  • iOS: Using Bluetooth audio output (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput) AudioSession
  • [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoExitFullScreen:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VideoEnterFullScreen:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
    

    However, since iOS8, I can’t achieve this. It is like the notification is no longer triggered from UIWebview videos. However, it is still triggered from normal videos, non-Webview, as I’ve tested.

    Any idea of what have changed?

    4 Solutions Collect From Internet About “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];
    

    For 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")
    }
    

    For swift:


    NotificationCenter.default.addObserver(self, selector: #selector(xxx), name: NSNotification.Name.MPMoviePlayerDidExitFullscreen, object: nil)

    @NorthBlast’s answer works well for detecting any UIWindow appearing on top of the UIViewController that holds the UIWebView. Unfortunately, it’s hard to filter what kind of UIWindow is (since, well… you can’t really know if it is a video or some other kind of window).

    There are 3 special cases I prefer to filter, in which you’re sure they are NOT video player windows, those are:

    1) _UIAlertControllerShimPresenterWindow, which is a kind of window that appears when using alerts (like UIAlertView).

    2) UITextEffectsWindow, which appears when presenting special iOS windows (like the share window, UIActivityViewController).

    3) UIRemoteKeyboardWindow which appears when presenting the keyboard (for some reason, this class only appeared to me when using Swift, but on Objective-C it didn’t… no clue why is that).

    So to subscribe to notifications, I use (just like @NorthBlast said):

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(windowDidBecomeActive:)
                                                 name:UIWindowDidBecomeVisibleNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(windowDidBecomeHidden:)
                                                 name:UIWindowDidBecomeHiddenNotification
                                               object:nil];
    

    Then the implementations:

    - (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;
    }