1. 程式人生 > >監聽UIWebView點選視訊播放的事件

監聽UIWebView點選視訊播放的事件

最近專案需要統計在跳轉UIWebView後,使用者點選網頁上的視訊進行播放的概率。找了很多監聽進入視訊播放的方法,最後在stackoverflow找到下面這個使用Notification的可行方法(如果是彈出):

#pragma mark Notification
- (void)addNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(beginPlayVideo:)
                                                 name:UIWindowDidBecomeVisibleNotification
                                               object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(endPlayVideo:)
                                                 name:UIWindowDidBecomeHiddenNotification
                                               object:self.view.window];
}

- (void)removeNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIWindowDidBecomeVisibleNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIWindowDidBecomeHiddenNotification
                                                  object:nil];
}
-(void)beginPlayVideo:(NSNotification *)notification{
    //如果是alertview或者actionsheet的話也會執行到這裡,所以要判斷一下
    if ([[UIApplication sharedApplication].keyWindow isMemberOfClass:[UIWindow class]]){
        [playButton removeFromSuperview];
    }
}


-(void)endPlayVideo:(NSNotification *)notification{
    NSLog(@"結束");
}