Discovery affects HTML5 <video> in UIWebView on iPhone

I would like to be able to detect touch / click events in a tag in a UIWebView. But it seems that he is not responding to this event. Any ideas?

videoElement.addEventListener('touchstart', myFunc, false); 

There may also be a 'click' event.

+4
source share
2 answers

There is a way to do this, but it is not very.

UIWebView allows you to run arbitrary javascript, for example. [webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"] . That way, when the user clicks on the video element, you can run javascript, which sets the innerHTML of a particular element, which you can extract from this function.

However, how do you know when to run this function on the Objective-C side? Well, there are two options. At first (although I would not suggest it, since it would be slow), you can set up a polling function that runs so often to check the value of the stringByEvaluatingJavascriptFromString: function. The second option, which, in my opinion, is perhaps the best, is to actually subclass WebView to track the touches of WebView. Thus, you can override the following function:

 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; NSString *string = [self stringByEvaluatingJavascriptFromString:@"document.getElementById('videoTapped').innerHTML"] if ([string isEqual:@"Something"]) { // do something here } } 
0
source

Configure your video element to load a specific URL when using it (using onclick or just a simple anchor), and then call -webView:shouldStartLoadWithRequest:navigationType: in your delegate to view that URL.

0
source

All Articles