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).
Artal source share