在UIWebView中拦截未使用​​的点击事件 [英] Intercept unused tap events in a UIWebView

查看:151
本文介绍了在UIWebView中拦截未使用​​的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个基于UIWebView的iPad应用程序:以最简单的方式解释它,该应用程序显示一个大的互动Web视图,此外它支持自定义手势。



我需要捕获代表webview上的单个点击的事件,但是只有当tapview还没有被webview消费时(即它们不是滚动/缩放操作的开始,它们不是



UIWebView是非常贪婪的事件,在我的经验,它倾向于不传播他们,甚至当他们没有消费。为了捕获事件,我最终对主UIWindow进行了子类化(见 http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/ )。这是很好的,但问题是我不能识别我获得的水龙头是否触发了一些javascript在网页视图或不。



作为一个额外的限制,我无法控制将要在UIWebView中运行的JavaScript或将要显示的HTML。



所以,问题是这样的:什么是一种方法来检测所有,只有tap事件,没有触发任何其他动作在UIWebView本身,特别是javascript动作?



$如果你好奇,我正在开发的项目的源代码是在GitHub:找到它,只要求Google指向你的Baker框架。

解决方案

使用JavaScript执行此操作。首先,在您的网页浏览完成加载后,请在身体上附加一个JavaScript点击处理程序,以吸收任何未使用的点击。让它加载一个补充域作为它的行动:

   - (void)webViewDidFinishLoad:(UIWebView *)webView 
{
NSString * script = @document.body.onclick = function(){document.location.href ='http:// clickhandler';}
[webView stringByEvaluatingJavaScriptFromString:script];
}

然后在您的webview代理中,注意加载域的尝试并拦截它们。现在,您可以使用本机代码拦截JavaScript单击处理程序,并相应地做出反应:

   - (BOOL)webView: *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
if([request.URL.host isEqualToString:@clickhandler])
{
// handle click
//你的逻辑在这里
return NO;
}
return YES;
}


I'm working on an iPad app which is based on a UIWebView: to explain it in the simplest possible terms, the app shows one big interactive webview and in addition it supports custom gestures.

I need to catch events that represent single taps on the webview, but only when the taps have not already been consumed by the webview (i.e. they are not the beginning of a scroll/zoom operation, they are not taps on links, they are not taps that trigger some javascript).

The UIWebView is very greedy with its events, and in my experience it tends not to propagate them, even when they are not consumed. To catch the events, I ended up subclassing the main UIWindow (see http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/). This is working well, but the problem is I'm not able to recognize whether the taps I'm getting have triggered some javascript in the webview or not.

As an additional restriction, I have no control over the javascript that's going to run in the UIWebView or the HTML that it's going to be displayed.

So, the question goes like this: what would be a way to detect all and only the tap events which did not trigger any other action in the UIWebView itself, especially javascript actions?

~

Should you be curious, the source code of the project I'm working on is on GitHub: to find it, just ask Google to point you to Baker Framework.

解决方案

Do this with JavaScript. First, after your webview finishes loading, attach a javascript click handler to the body to soak up any unused clicks. Have it load a made-up domain as it's action:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *script = @"document.body.onclick = function () { document.location.href = 'http://clickhandler'; }"
    [webView stringByEvaluatingJavaScriptFromString:script];
}

Then in your webview delegate, look out for attempts to load that made up domain and intercept them. You now have a way to intercept that javascript click handler with native code and react accordingly:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.host isEqualToString:@"clickhandler"])
    {
        //handle click
        //your logic here
        return NO;
    }
    return YES;
}

这篇关于在UIWebView中拦截未使用​​的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆