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

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

问题描述

我有一个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内的Button点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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