Javascript 如何在IOS上拦截UIWebview内的Button点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10992339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to intercept Button click inside UIWebview on IOS?
提问by B K
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.
我有一个 HTML 表单,其中包含我在 UIWebview 中打开的某些字段。单击特定按钮时,我想执行应用程序内操作。
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?
我现在使用的方法是单击按钮将页面重定向到一个虚拟 URL。我使用委托方法“shouldStartLoadWithRequest”嗅探 URL 并停止重定向。然后我做我的自定义事件(捕获图像并上传),然后继续。这似乎是一个丑陋的黑客。无论如何,我可以直接挂钩按钮单击事件而不是页面重定向?
UPDATEThere 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.
更新当单击按钮时调用 JS 函数时,似乎无法设置委托方法来触发。唯一的方法是使用上面提到的 URL 嗅探方法。joern在下面详细解释了这一点,所以我会接受他的回答。
回答by joern
You can do the following:
您可以执行以下操作:
In your HTML
在你的 HTML
<a class="yourButton" href="inapp://capture">Button Text</a>
In your UIWebViewDelegate
在你的 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;
}
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):
我在按钮的 URL 中添加了“捕获”,以便您可以区分应该触发不同操作的几个按钮(如果需要):
回答by Malder
This code:
这段代码:
<a class="yourButton" href="inapp://capture">Restart</a>
doesn't work for me.
对我不起作用。
I've added "inapp://capture"in my javascript code for event function
我在我的 javascript 代码中为事件函数添加了“inapp://capture”
KeyboardInputManager.prototype.restart = function (event) {
window.location.href = "inapp://capture";
//other code
};
Also I use the UIWebViewDelegate and It works for iOS 8, 9.
我也使用 UIWebViewDelegate 并且它适用于 iOS 8、9。
回答by akonoplev
For swift 4.2
对于快速 4.2
Button code in HTML side:
HTML端的按钮代码:
<a class="catch-iOS" href="inapp://home">Продолжить покупки</a>
UIWebViewDelegate method:
UIWebViewDelegate 方法:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
if request.url?.scheme == "inapp" {
if request.url?.host == "home" {
// do something
}
}
return true
}