简体   繁体   中英

Autofill username and password in an HTML page opening in UIWebView

I'm saving the username and password data as entered by the user in an HTML page opened in UIWebView. Now, what i want to do is to put username and password back into their respective fields and submit the form and I want this functionality to work for almost any kind of website.

Currently, I'm able to put data back into their respective fields using JavaScript. My problem is how to identify the form filled with values when there are more than one form on a web page.

Also, during the development of my application I figured out that some web sites for security reasons dont let populate the password field. What JavaScript code should i use to implement this functionality?

Hey Man I hope this Will Help You in Web View.

   - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
            //For Authentication
        NSString *savedUsername = @"Bapu";
        NSString *savedPassword = @"Bapu123";

        if (savedUsername.length != 0 && savedPassword.length != 0)
        {
         //create js strings
         NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                                                 for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
        NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='password']\"); \
                                        for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];

             //autofill the form
        [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
        [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
        }
    }

In short the only answer I can think of is java script. Something like, assuming your UIWebView object is *webView. Make the class support UIWebViewDelegate, then assign the delegate somewhere appropriate:

// In viewDidLoad or somewhere appropriate.
self.webView.delegate = self;

UIWebView delegate function.

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
    NSString *fillAndSubmit = [NSString stringWithFormat:@"<--- javascript --->",username,password];
    NSString *output = [webView stringByEvaluatingJavaScriptFromString:fillAndSubmit];
    // Any other stuff you need to do.
}

The above is a rough outline, replace <--- javascript ---> with javascript code that will fill the username and password into the relevant form and submit it. If the page that's loaded uses, say, JQuery you can use JQuery in the javascript here.

As for the javascript to use, well that's dependent totally on the web page in question so I can really help there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM