I\'m using UIWebView
to play YouTube videos in an iPad.
How can I detect when a YouTube video is finished playing? I see the play icon in the status ba
No, there's no way to directly get web page event from UIWebView
. But we can accomplish this by using Javascript.
These links may help:
Calling Objective-C from JavaScript in an iPhone UIWebView
Javascript callback in Objective-C
YouTube iFrame API
updated with an example:
in UIWebView's delegate, i put:
#pragma - mark WebView Delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ( [[[request URL] scheme] isEqualToString:@"callback"] ) {
NSLog(@"get callback");
return NO;
}
return YES;
}
the web page is load when viewDidLoad
:
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"youtube" ofType:@"html" ]]]];
and in youtube.html i put:
you can see i add
if (event.data == YT.PlayerState.ENDED) {
window.location = "callback:anything";
};
to YouTube's iFrame API demo, and it catches the player end playing event and try to load a request with scheme "callback", then the UIWebView Delegate could catch it.
You can use this method to trigger any event using JavaScript;