xcode 在 UIWebView 中自动填充用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35140030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autofill Username and Password in UIWebView
提问by Matte.Car
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.
我正在为我做一个非常简单的应用程序。当我启动这个应用程序时,它只是把我带到这个网页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?
我想自动登录,所以有没有办法自动填充用户名和密码,选中“我接受条件”标记并按下“登录”按钮?
I tried this Autofill Username and Password UIWebView Swiftbut it doesn't work, I'm not the creator of the page so I don't know exactly how it's structured.
我试过这个Autofill Username and Password UIWebView Swift但它不起作用,我不是页面的创建者,所以我不知道它的结构。
Would be cool even if iCloud keychains would fill the forms...but maybe I'm asking too much!
即使 iCloud 钥匙串可以填写表格也会很酷……但也许我要求太多了!
回答by Artal
You can use JavaScript to do it.
您可以使用 JavaScript 来做到这一点。
If you inspect the page, you can pretty easily spot the IDs of the text input fields (expert_emailand expert_password) and execute some JS code to fill them.
如果您检查页面,您可以很容易地发现文本输入字段(expert_email和expert_password)的 ID,并执行一些 JS 代码来填充它们。
Implement the webViewDidFinishLoadmethod of the UIWebViewdelegate like so:
像这样实现委托的webViewDidFinishLoad方法UIWebView:
OBJECTIVE-C:
目标-C:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//fill data
NSString *email = @"[email protected]";
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.
这应该填写您的信息。但是,如果此网站将来更改其结构,则此代码很容易被破坏。这种执行JS代码的方式在处理自己的页面时更合理。
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
webViewDidFinishLoadsince 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
webViewDidFinishLoadanyway 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).
- 当登录失败时,页面会重新加载,因此
webViewDidFinishLoad由于您试图一遍又一遍地提交,因此您最终会无限循环调用,因此在这种情况下您可能需要添加一些逻辑来中断。这就是为什么我在提交之前延迟以便能够看到发生了什么。 - 除了上一点中的信息 - 您将
webViewDidFinishLoad在登录成功后(重定向到下一页时)无论如何都会收到调用,因此您可能希望在加载不同的页面后提升标志(仅执行登录尝试在登录页面时)。

