如何在IOS上拦截UIWebview内的按钮点击? [英] how to intercept Button click inside UIWebview on IOS?

查看:17
本文介绍了如何在IOS上拦截UIWebview内的按钮点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 HTML 表单,其中包含我在 UIWebview 中打开的某些字段.单击特定按钮时,我想做一个应用内操作.

I have a HTML form that has certain fields which i am opening inside a UIWebview. On click of a particular button i want to do a in app action.

我现在使用的方法是单击按钮,将页面重定向到虚拟 URL.我使用委托方法shouldStartLoadWithRequest"嗅探 URL 并停止重定向.然后我执行我的自定义事件(捕获图像并上传),然后继续.这似乎是一个丑陋的黑客.无论如何我可以直接挂钩按钮点击事件而不是页面重定向?

The approach i'm using now is on click of the button i redirect the page to a dummy URL. I sniff the URL using delegate method "shouldStartLoadWithRequest" and stop the redirection. I then do my custom event (capture image and upload) and then continue. This seems to be an ugly hack. Anyway i can directly hook into the button click event rather than a page redirection?

更新似乎没有办法设置在单击按钮时调用 JS 函数时触发的委托方法.唯一的方法是使用上面提到的 URL 嗅探方法.这一点joern在下面已经详细解释过了,所以我会接受他的回答.

UPDATE There seems to be no way to setup a delegate method to fire when a JS function is called on button click. The only way to do this is to use the URL sniffing method mentioned above. This has been explained in detail by joern below, so I will accept his answer.

推荐答案

您可以执行以下操作:

在您的 HTML 中

<a class="yourButton" href="inapp://capture">Button Text</a>

在你的 UIWebViewDelegate 中

In your UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
   if ([request.URL.scheme isEqualToString:@"inapp"]) {
      if ([request.URL.host isEqualToString:@"capture"]) {
         // do capture action
      }  
      return NO;
   }  
   return YES;
}

我在按钮的 URL 中添加了捕获",以便您可以区分应该触发不同操作的几个按钮(如果您愿意的话):

I added "capture" to your button's URL so that you could distinguish between several buttons that should trigger different actions (if you want to):

这篇关于如何在IOS上拦截UIWebview内的按钮点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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