Autofill Username and Password in UIWebView

走远了吗. 提交于 2019-12-05 08:06:55
Artal

You can use JavaScript to do it.

If you inspect the page, you can pretty easily spot the IDs of the text input fields (expert_email and expert_password) and execute some JS code to fill them.

Implement the webViewDidFinishLoad method of the UIWebView delegate like so:

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 your info. However, this code can easily break if this website changes its structure in the future. This method of executing JS code is more reasonable when you're handling your own pages.

Edit:

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

Some notes:

  • when the login fails the page is reloaded, so you're going to end up with an endless loop of calls to webViewDidFinishLoad since you're trying to submit over and over again, so you might want to add some logic to break in this case. That's why I put a delay before submitting in order to be able to see what's going on.
  • In addition to the info in the previous point - you're going to get calls to webViewDidFinishLoad anyway after login is successful (when redirected to the next page) so you might want to raise a flag once a different page loads (perform the login attempt only when on the login page).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!