简体   繁体   English

检测用户何时单击UIWebView或WKWebView中加载的Web窗体

[英]Detecting when a user clicks on a webform loaded in a UIWebView or WKWebView

I'd like to detect when a user presses a webform button present in an HTML page loaded in an iOS webview (ideally using WKWebView). 我想检测用户何时按下iOS Webview中加载的HTML页面中存在的Webform按钮(最好使用WKWebView)。 Is this possible? 这可能吗? And if so, how ? 如果是这样, 怎么办?

General comment: 一般评论:

I know that WKWebView has some limitations hence if you know a method that works with WKWebView please share it. 我知道WKWebView有一些限制,因此,如果您知道可与WKWebView一起使用的方法,请共享它。 If you know a method that works with UIWebView please share that as well as I can use it as start to find a solution for my WKWebView based app. 如果您知道可以与UIWebView一起使用的方法,请与我分享该方法,并以此作为开始为基于WKWebView的应用程序找到解决方案的方法。

To do so using WKWebView, you need to conform to WKNavigationDelegate , and set the webView's navigationDelegate . 要使用WKWebView进行此操作,您需要遵循WKNavigationDelegate ,并设置webView的navigationDelegate

The method you're probably looking for is - webView:decidePolicyForNavigationAction:decisionHandler: , where you can determine whether to allow the link touch or not. 您可能正在寻找的方法是- webView:decidePolicyForNavigationAction:decisionHandler: ,您可以在其中确定是否允许链接触摸。 You can get the target URL from navigationAction.request.URL . 您可以从navigationAction.request.URL获取目标URL。

A quick example: 一个简单的例子:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    if ([navigationAction.request.URL.relativeString hasPrefix:kLocalContentPrefix])
    {
        // Show some local content

        // Don't let the webview load this URL.
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    else
    {
        // Allow the webview to load this URL.
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

To do this in UIWebView, it's almost the same, except you just conform to -(BOOL)webView:shouldStartLoadWithRequest:navigationType: in UIWebViewDelegate . 要在UIWebView中执行此操作,几乎是一样的,只是您要遵循UIWebViewDelegate中的 -(BOOL)webView:shouldStartLoadWithRequest:navigationType: But I would recommend using Webkit if possible! 但是我建议尽可能使用Webkit!

有一种解决方法,由shazron在Objective-C的https://github.com/shazron/WKWebViewFIleUrlTest上演示,可以将文件复制到/ tmp / www并从那里加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM