Intercept unused tap events in a UIWebView

拥有回忆 提交于 2019-12-05 15:57:08

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!