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并停止重定向。然后我做我的自定义事件(捕获图像和上传),然后继续。这似乎是一个丑陋的黑客。无论如何,我可以直接挂钩按钮点击事件而不是页面重定向?
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.
更新当按钮单击调用JS函数时,似乎无法设置要触发的委托方法。唯一的方法是使用上面提到的URL嗅探方法。这个已经由joern在下面详细解释过,所以我会接受他的回答。
2 个解决方案
#1
55
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中,以便您可以区分应触发不同操作的几个按钮(如果您愿意):
#2
3
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。
#1
55
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中,以便您可以区分应触发不同操作的几个按钮(如果您愿意):
#2
3
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。