Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView checking for HTML form submission

I have been tasked with converting an Objective-C app to fully Swift 4. The app is basically a webbroswer that wraps my companies website. To keep the users form complaining about entering their username / password each time, the app needs to check when an HTML form was submitted and scrape the site and store the u/p.

In ObjC there was a method: - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

That allowed me to check when a form was submitted:

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;
    }

But I am having trouble converting that Swift 4.1 as I can't seem to find any type of UIWebViewNavigationTypeFormSubmitted for WKWebView.

Any help?

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

    }
}
like image 388
user-44651 Avatar asked Aug 09 '26 18:08

user-44651


1 Answers

It should be

if navigationType == UIWebViewNavigationType.formSubmitted

or

if navigationType == .formSubmitted
like image 149
trungduc Avatar answered Aug 11 '26 15:08

trungduc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!