【问题标题】:WKWebView checking for HTML form submissionWKWebView 检查 HTML 表单提交
【发布时间】:2018-06-12 17:37:08
【问题描述】:

我的任务是将 Objective-C 应用程序转换为完全 Swift 4。该应用程序基本上是一个包含我公司网站的网络浏览器。为了让用户表单每次都抱怨输入他们的用户名/密码,应用程序需要检查何时提交 HTML 表单并抓取站点并存储 u/p。

在 ObjC 中有一个方法: - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

这让我可以检查表单何时提交:

static NSString * const FORM_USER = @"document.getElementById('sUserID').value";
static NSString * const FORM_PASS = @"document.getElementById('sPassword').value";

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

        //save form data
        if(navigationType == UIWebViewNavigationTypeFormSubmitted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                //grab the data from the page
                NSString *username = [self.webView stringByEvaluatingJavaScriptFromString: FORM_USER];
                NSString *password = [self.webView stringByEvaluatingJavaScriptFromString: FORM_PASS];

                //store values locally in the background
                //.....
            });

        }
        return true;
    }

但我在转换 Swift 4.1 时遇到了麻烦,因为我似乎无法为 WKWebView 找到任何类型的 UIWebViewNavigationTypeFormSubmitted

有什么帮助吗?

let FORM_USER = "document.getElementById('sUserID').value"
let FORM_PASS = "document.getElementById('sPassword').value"

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation) {

    if(webView.url == API_LOGIN_URL){

        if(WHAT A DO I CHECK FOR here?){

            //Grab IN LOGIN DETAILS
            var name = webView.evaluateJavaScript("\(FORM_USER)", completionHandler: nil)
            var pass = webView.evaluateJavaScript("\(FORM_PASS)", completionHandler: nil)

            //store values locally
            //...
        }

    }
}

【问题讨论】:

    标签: ios objective-c swift swift4 wkwebview


    【解决方案1】:

    应该是

    if navigationType == UIWebViewNavigationType.formSubmitted
    

    if navigationType == .formSubmitted
    

    【讨论】:

      猜你喜欢
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多