9

Before iOS 8, the UIMoviePlayerControllerDidEnterFullscreenNotification notification was sent any time a media player went to fullscreen from a UIWebView. In iOS 8, this doesn't happen and some have suggested to listen for the AVPlayerItemBecameCurrentNotification notification instead. This doesn't appear to be sent from WKWebView. Listening for the UIWindowDidBecomeVisibleNotification notification doesn't work because it's fired for all windows that are added (including things like ad networks)

Bottom line, I've been working on this all night and I can't seem to figure out how to determine if a video was opened in full screen with a WKWebView. Any help would be appreciated.

Edit: To confirm, I created a blank project. Added a UIWebView and the AVPlayerItemBecameCurrentNotification listener to it and it got triggered when I played a video and it entered full screen. I switched that UIWebView to a WKWebView and that notification was no longer triggered.

Steve E
  • 792
  • 10
  • 16
  • The best I could find was the `UIWindowDidBecomeKeyNotification` for determining when another window took over. This, luckily, didn't fire when my banner ad was created. I then set a `windowBecameKey` variable and tested for it on the `UIWindowDidBecomeHiddenNotification`. The only hiccup I saw that could make this a little fragile is that `UIWindowDidBecomeKeyNotification` fires again just before the fullscreen window hides. If, for some reason, it fires afterwards, it would probably break things. – Steve E Dec 11 '14 at 17:42
  • can find any the actually avplayeritem instance from WKWebView https://stackoverflow.com/questions/55377677/how-to-detect-avplayer-and-get-url-of-current-video-from-wkwebview – Virani Vivek Apr 04 '19 at 12:36

2 Answers2

4

This workaround seems to work on iOS8 & iPhone 6

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ...

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

    return TRUE;
}

- (void)windowBecameHidden:(NSNotification *)notification {

    UIWindow *window = notification.object;

    if (window != self.window) {    // Not my own window: assuming the video window was hidden, maybe add some more checks here.

            // Add code here
    }
}
  • Detect when avplayer show and play can you help me please see this https://stackoverflow.com/questions/55377677/how-to-detect-avplayer-and-get-url-of-current-video-from-wkwebview – Virani Vivek Apr 03 '19 at 05:48
0

I just need to do the same. I listened all notifications with this answer https://stackoverflow.com/a/7955014/1271424 and found: there are no notifications about fullscreen mode at all, except notifications about creating new window (_UIWindowContentWillRotateNotification) and about MPRemote (MPRemoteCommandTargetsDidChangeNotification).

Tested on iPad, 8.1.1

Community
  • 1
  • 1
Roman Truba
  • 4,401
  • 3
  • 35
  • 60