WKWebView 检查 HTML 表单提交

use*_*651 3 objective-c ios swift wkwebview swift4

我的任务是将 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;
    }
Run Code Online (Sandbox Code Playgroud)

但我在转换 Swift 4.1 时遇到了麻烦,因为我似乎找不到UIWebViewNavigationTypeFormSubmittedWKWebView 的任何类型。

有什么帮助吗?

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
            //...
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

tru*_*duc 5

它应该是

if navigationType == UIWebViewNavigationType.formSubmitted
Run Code Online (Sandbox Code Playgroud)

或者

if navigationType == .formSubmitted
Run Code Online (Sandbox Code Playgroud)