代码之家  ›  专栏  ›  技术社区  ›  user-44651

WKWebView检查HTML表单提交

  •  1
  • user-44651  · 技术社区  · 8 年前

    我的任务是将objective-c应用程序转换为fully swift 4。这个应用程序基本上是一个webbroswer,它可以封装我的公司网站。为了让用户每次都抱怨输入了用户名/密码,应用程序需要检查何时提交了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,因为我似乎找不到任何类型的 UIWebViewNavigationTypeFormSubmitted 对于wkwebview。

    有什么帮助吗?

    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
                //...
            }
    
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   trungduc 邱林和    8 年前

    应该是

    if navigationType == UIWebViewNavigationType.formSubmitted
    

    或

    if navigationType == .formSubmitted