在 UIWebView 中打开的 HTML 页面中自动填充用户名和密码 [英] autofill username and password in an HTML page opening in UIWebView
问题描述
如何在使用 UIWebView 自动填充数据时使用 RegsEx 查找登录表单?
How to find login form using RegsEx when using UIWebView to autofill data ?
我使用以下代码来获取所有表单数据:
I have used following code to get all form data:
int NoOfForms = [[self.browser stringByEvaluatingJavaScriptFromString:@"document.forms.length"] intValue];
NSMutableArray *formData = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0 ; i < NoOfForms ; i++)
{
// total number of elements in each form
int elementsInForm = [[self.browser stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.forms[%d].elements.length",i]] intValue];
for (int j = 0; j < elementsInForm; j++)
{
// element id of each element
NSString *elementID = [NSString stringWithFormat:@"document.forms[%d].elements[%d].id",i,j];
// element value of each element
NSString *elementValue = [NSString stringWithFormat:@"document.forms[%d].elements[%d].value",i,j];
// dictionary for all values of each element
NSDictionary *elementData = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:i],@"form No",
[NSNumber numberWithInt:j],@"element No",
[self.browser stringByEvaluatingJavaScriptFromString:elementID],@"elementID",
[self.browser stringByEvaluatingJavaScriptFromString:elementValue],@"elementValue",nil];
// add each element data to a mutable array.
[formData addObject:elementData];
}
}
并使用以下代码将数据放回各自的字段:
And have the following code to put data back into their respective fields:
for (int i = 0 ; i < [dataArray count]; i++)
{
NSDictionary *elementDict = [dataArray objectAtIndex:i];
int formNumber = [[elementDict objectForKey:@"form No"] intValue];
int elementNumber = [[elementDict objectForKey:@"element No"] intValue];
NSString *formElementID = [elementDict objectForKey:@"elementID"];
NSString *formElementValue = [elementDict objectForKey:@"elementValue"];
if ([[self.browser stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.forms[%d].elements[%d].id",formNumber,elementNumber]] isEqualToString:formElementID])
{
[self.browser stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.forms[%d].elements[%d].value='%@'",formNumber,elementNumber,formElementValue]];
}
}
现在我只想提交登录表单..如何找到它?
Now i want to submit only the login form..how to find it ?
我遇到了另一个问题:当我的代码将密码放入其字段时,一些网站(例如 mail.google.com)确实允许这种情况发生,即它会清空密码字段.如何解决这个问题?
I'm experiencing one more problem: when my code puts password into its field, some web sites like mail.google.com does let this happen i.e. it empties the password field. How to solve this problem ?
更新:目前我已经能够登录 mail.google.com,但无法登录 belleandclive.com.此站点是否对其登录表单实施了任何安全措施?
UPDATE : currently i have been able to log in on mail.google.com but could not get done the same for belleandclive.com . Is this site implementing any security measurements for its login form ?
推荐答案
我觉得你可以根据表单数据或者表单id来判断哪个是登录表单,对吧?
I think you can judge which is the login form according to the form data or form id, right?
所以,如果登录表单id是login-form
,可以使用如下代码提交:
So, if the login form id is login-form
, you can use the following code to submit it:
NSString * js = @"document.getElementById('login-form').submit()";
[self.webview stringByEvaluatingJavaScriptFromString:js];
也可以通过表单数据判断哪个索引是登录表单来提交表单:
or you can sumbit the form through judging which index is login form according the form data:
int index = ....// your judge code to get index
NSString * js = [NSString stringWithFormat:@"document.forms[%d].submit()", index];
[self.webview stringByEvaluatingJavaScriptFromString:js];
这篇关于在 UIWebView 中打开的 HTML 页面中自动填充用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!