2014-09-10 10 views
12

Ho un'app in cui gli utenti possono aprire video da UIWebview, inclusi quelli di YouTube. In iOS7, sono stato in grado di ricevere una notifica quando è iniziata la riproduzione o quando è diventato a schermo intero, il che è fondamentale per me per mostrare determinate opzioni all'utente e modificare l'interfaccia.Rileva quando un video webview diventa a schermo intero su ios8

Ho usato per usare questo:

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

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

Tuttavia, dal momento che iOS 8, non riesco a raggiungere questo obiettivo. È come se la notifica non fosse più attivata dai video di UIWebview. Tuttavia, è ancora attivato dai normali video, non Webview, come ho provato.

Qualche idea di cosa è cambiato?

risposta

19

Questo è il lavoro in giro ho trovato per questo ..

[[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]; 
+3

ho trovato che ** ** UIWindowDidBecomeVisibleNotification viene chiamato quando il video inizia a giocare a schermo intero. E ** UIWindowDidBecomeHiddenNotification ** viene chiamato quando il video scompare. –

+0

@IgorKulagin che è corretto. – NorthBlast

+0

Mi hai salvato la notte :) –

5

Per 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") 
} 
0

per SWIFT:

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

+0

Questo non funzionerà più per iOS 9, ecc. Ora è deprecato. – Tobonaut

0

@ risposta di NorthBlast funziona bene per rilevare qualsiasi UIWindow visualizzato in cima allo UIViewController che contiene lo UIWebView. Sfortunatamente, è difficile filtrare il tipo di UIWindow (poiché, beh ... non puoi sapere se si tratta di un video o di un altro tipo di finestra).

Ci sono 3 casi speciali preferisco filtrare, in cui siete sicuri che siano NON video di Windows giocatore, questi sono:

1) _UIAlertControllerShimPresenterWindow, che è una sorta di finestra che appare quando si usa avvisi (come UIAlertView).

2) UITextEffectsWindow, visualizzato quando si presentano finestre speciali iOS (come la finestra di condivisione, UIActivityViewController).

3) UIRemoteKeyboardWindow che appare quando si presenta la tastiera (per qualche ragione, questa classe mi è apparsa solo quando si usa Swift, ma su Objective-C non ha ... nessun indizio perché è così).

Quindi, per sottoscrivere le notifiche, io uso (proprio come ha detto @NorthBlast):

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

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

Poi le implementazioni:

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