Auto-complete username and password in UIWebView

I am making a very lightweight application for me. When I launch this application, it simply leads me to this web page https://social.tre.it/expert .

I would like to automate the login, so is there a way to autofill the username and password, check the box "I accept the condition" and click the "Login" button?

I tried this UIWebView Swift username and password autocomplete , but this will not work, I am not the page creator, so I don’t know exactly how it is structured.

It would be great even if iCloud key rings fill out forms ... but maybe I ask too much!

+5
source share
1 answer

You can use JavaScript for this.

If you are browsing a page, you can easily determine the identifiers of the text input fields ( expert_email and expert_password ) and execute some JS code to populate them.

Implement the webViewDidFinishLoad method of the webViewDidFinishLoad delegate as follows:

OBJECTIVE-C:

 - (void)webViewDidFinishLoad:(UIWebView *)webView { //fill data NSString *email = @" myemail@email.com "; NSString *password = @"mypassword"; NSString *fillDataJsCall = [NSString stringWithFormat:@"document.getElementById('expert_email').value = '%@';document.getElementById('expert_password').value = '%@';", email, password]; [webView stringByEvaluatingJavaScriptFromString:fillDataJsCall]; //check checkboxes [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('expert_remember_me').checked = true; document.getElementById('expert_terms_of_service').checked = true;"]; //submit form dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) { [webView stringByEvaluatingJavaScriptFromString:@"document.forms[\"new_expert\"].submit();"]; }); } 

SWIFT:

 func webViewDidFinishLoad(webView: UIWebView) { // fill data let savedUsername = "USERNAME" let savedPassword = "PASSWORD" let fillForm = String(format: "document.getElementById('expert_email').value = '\(savedUsername)';document.getElementById('expert_password').value = '\(savedPassword)';") webView.stringByEvaluatingJavaScriptFromString(fillForm) //check checkboxes webView.stringByEvaluatingJavaScriptFromString("document.getElementById('expert_remember_me').checked = true; document.getElementById('expert_terms_of_service').checked = true;") //submit form dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * NSEC_PER_SEC)), dispatch_get_main_queue()){ webView.stringByEvaluatingJavaScriptFromString("document.forms[\"new_expert\"].submit();") } } 

This should fill in your information. However, this code can easily break if this website changes its structure in the future. This JS code execution method is more smart when you process your own pages.

Edit:

I updated the code to demonstrate how to check the checkboxes and how to submit the form.

Some notes:

  • If the login attempt fails, the page reloads, so you get an endless loop of webViewDidFinishLoad calls, because you are trying to send again and again, so you may need to add some logic to break in this case. That's why I set a delay before sending in order to be able to see what was happening.
  • In addition to the information in the previous paragraph, you will receive calls to webViewDidFinishLoad in any case after a successful login (when redirecting to the next page) so that you can raise the flag when the page is loaded differently (try to log in only on the login page into the system).
+10
source

Source: https://habr.com/ru/post/1242037/


All Articles