最近项目需要统计在跳转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(@"结束");
}