在UIWebView的HTML页中打开自动填写用户名和密码(autofill username an

2019-09-16 09:27发布

如何使用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]];
     }

}

现在我只想提交登录form..how找到它?

我遇到更多的一个问题:当我的代码把密码进入该领域,一些网站如mail.google.com不会让这种情况发生,即它倒空密码字段。 如何解决这个问题呢 ?

更新:目前我已经能够登录在mail.google.com但却无法做了belleandclive.com相同。 这是网站实现其登录表单任何的安全措施?

Answer 1:

我认为你可以判断这是根据表格数据或形式ID登录表单,对不对?

所以,如果登录表单ID是login-form ,可以使用下面的代码提交:

NSString * js = @"document.getElementById('login-form').submit()";
[self.webview stringByEvaluatingJavaScriptFromString:js];

或者也可以通过透过其判断指数是根据表格数据登录表单的形式:

int index = ....// your judge code to get index
NSString * js = [NSString stringWithFormat:@"document.forms[%d].submit()", index];
[self.webview stringByEvaluatingJavaScriptFromString:js];


文章来源: autofill username and password in an HTML page opening in UIWebView