使用 UIWebView 自动填充数据时如何使用 RegsEx 查找登录表单?
我使用以下代码获取所有表单数据:
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];
}
}
并具有以下代码将数据放回各自的字段:
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]];
}
}
现在我只想提交登录表单..如何找到它?
我遇到了另一个问题:当我的代码将密码放入其字段时,某些网站(如 mail.google.com)确实让这种情况发生,即它清空了密码字段。如何解决这个问题呢 ?
更新:目前我已经能够登录 mail.google.com,但无法在 belleandclive.com 上进行同样的操作。该站点是否对其登录表单实施任何安全措施?