在UIWebView中自动填充用户名和密码 [英] Autofill Username and Password in UIWebView

查看:118
本文介绍了在UIWebView中自动填充用户名和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我做一个非常简单的应用程序。当我启动这个应用程序时,它只是将我带到这个网页 https://social.tre.it/expert

I'm doing a very easy app for me. When I launch this app, it simply bring me to this web page https://social.tre.it/expert.

我想自动登录,所以有办法自动填写用户名和密码,检查我接受条件标记并按登录按钮?

I would like to automatise login so is there a way to autofill username and password, check the "I accept condition" mark and push "login" button?

我试过这个 Autofill用户名和密码UIWebView Swift 但它不起作用,我不是页面的创建者所以我不确切知道它的结构。

I tried this Autofill Username and Password UIWebView Swift but it doesn't work, I'm not the creator of the page so I don't know exactly how it's structured.

即使iCloud钥匙串填写表格也会很酷......但也许我要求太多了!

Would be cool even if iCloud keychains would fill the forms...but maybe I'm asking too much!

推荐答案

你可以使用JavaScript来完成它。

You can use JavaScript to do it.

如果你检查页面,你可以很容易地发现文本输入字段的ID( expert_email expert_password )并执行一些JS代码填写它们。

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.

实现 UIWebView的 webViewDidFinishLoad 方法如此委托:

Implement the webViewDidFinishLoad method of the UIWebView delegate like so:

目标-C:

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:

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();")
    }
}

这应填写您的信息。但是,如果此网站将来更改其结构,则此代码很容易破解。当您处理自己的页面时,这种执行JS代码的方法更合理。

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.

编辑:

我更新了代码以演示如何选中复选框以及如何提交表单。

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

一些注释:


  • 当登录失败时页面被重新加载,所以你最终将无限循环调用 webViewDidFinishLoad ,因为你试图反复提交,所以你可能想要添加一些逻辑来打破在这种情况下。这就是为什么我在提交之前提出延迟以便能够看到发生了什么。

  • 除了上一点的信息之外 - 你将接到<$的电话c $ c> webViewDidFinishLoad 无论如何在登录成功后(重定向到下一页时),因此您可能希望在加载不同页面时引发标志(仅在登录页面上执行登录尝试) 。

  • 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).

这篇关于在UIWebView中自动填充用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆