简体   繁体   English

拦截UIWebView中未使用的tap事件

[英]Intercept unused tap events in a UIWebView

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. 我正在开发一个基于UIWebView的iPad应用程序:用最简单的术语解释它,该应用程序显示了一个大的交互式webview,此外它还支持自定义手势。

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 (ie they are not the beginning of a scroll/zoom operation, they are not taps on links, they are not taps that trigger some javascript). 我需要在webview上捕获代表单击的事件,但只有当webview尚未使用点击时(即它们不是滚动/缩放操作的开始,它们不是点击链接,它们不是点击触发一些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. UIWebView对其事件非常贪婪,根据我的经验,它往往不会传播它们,即使它们没有被消耗。 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/ ). 为了捕获事件,我最终继承了主UIWindow(请参阅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. 这很好用,但问题是我无法识别我所获得的水龙头是否在webview中触发了一些javascript。

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. 作为一个额外的限制,我无法控制将在UIWebView或将要显示的HTML中运行的javascript。

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? 所以,问题是这样的:什么是一种方法来检测所有只有在UIWebView本身没有触发任何其他动作的点击事件,尤其是javascript动作?

~

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. 如果您感到好奇,我正在研究的项目的源代码是在GitHub上:找到它,只要求Google指向Baker Framework。

Do this with JavaScript. 用JavaScript做到这一点。 First, after your webview finishes loading, attach a javascript click handler to the body to soak up any unused clicks. 首先,在webview完成加载后,将javascript单击处理程序附加到正文以吸收任何未使用的点击。 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. 然后在您的webview委托中,查看是否尝试加载组成域并拦截它们。 You now have a way to intercept that javascript click handler with native code and react accordingly: 您现在有办法拦截具有本机代码的javascript单击处理程序并做出相应的反应:

- (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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM