I have a view with a web view which loads a YouTube video. I am using the following methods to automatically start the YouTube video when the web view is loaded.
The web view opens the iPhones native movie player. Is there any way to check if the video has ended or the user has pushed the "OK" button of the movie player and the player is thereby closed?
These are the methods I use for automatically starting the web view:
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if(view.subviews && [view.subviews count] > 0) {
for(UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if(button) return button;
}
}
return button;
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
UIButton *b = [self findButtonInView:_webView];
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
Apple doesn't push a [documented] notification for this, so you have to get a little tricky.
The way I do it is to check the application's keyWindow. I got the idea from here.
in your .h file, keep track of your timer and the desired keyWindow:
NSTimer *windowTimer;
UIWindow *keyWindow;
in your .m file, you need the following:
- (void)viewDidLoad {
[super viewDidUnload];
keyWindow = [[UIApplication sharedApplication] keyWindow];
}
Then Edit your delegate method and add one new method:
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
UIButton *b = [self findButtonInView:_webView];
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
// start checking the current keyWindow
windowTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(checkWindowStatus) userInfo:nil repeats:YES];
}
- (void) checkWindowStatus {
// if the key window is back to our application
if (keyWindow == [[UIApplication sharedApplication] keyWindow]) {
[windowTimer invalidate];
windowTimer = nil;
... window has dismissed, do your thing ...
}
}