autofill username and password in an HTML page opening in UIWebView

僤鯓⒐⒋嵵緔 提交于 2020-01-04 06:23:07

问题


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 ?

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 ?

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 ?


回答1:


I think you can judge which is the login form according to the form data or form id, right?

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];


来源:https://stackoverflow.com/questions/10039602/autofill-username-and-password-in-an-html-page-opening-in-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!