开发者

Detecting touches on an HTML5 <video> in a UIWebView on the iPhone

开发者 https://www.devze.com 2023-01-15 03:46 出处:网络
I would like to be able to detect touch/click events on thetag in the UIWebView. But it doesn\'t look like it responds to that event. Any ideas?

I would like to be able to detect touch/click events on the tag in the UIWebView. But it doesn't look like it responds to that event. Any ideas?

videoElement.addEventListener('touchs开发者_运维技巧tart', myFunc, false);

Could also be the 'click' event.


There is a way to do this, but it isn't pretty.

UIWebView allows you to run arbitrary javascript, e.g. [webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"]. So, when the user taps on the videoElement, you can have a javascript run which sets the innerHTML of a specific element, which you can grab from this function.

However, how do you know when to run this function on the Objective-C side? Well, there are two options. First (though I wouldn't suggest it since it would be slow), you can set up a polling function which runs every so often to check the value of the stringByEvaluatingJavascriptFromString: function. The second option, which I think is probably the best one, is to actually subclass WebView to watch touches on the WebView. So, 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
    }
} 


Set up your video element to load a particular URL when tapped (using onclick or just a plain anchor), then implement the -webView:shouldStartLoadWithRequest:navigationType: callback in your webview's delegate to watch for that URL.

0

精彩评论

暂无评论...
验证码 换一张
取 消