2

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 ?

Manish Verma
  • 41
  • 1
  • 5
  • Check http://stackoverflow.com/questions/4772341/is-it-possible-for-a-uiwebview-to-save-and-autofill-previously-entered-form-valu – msk Apr 05 '12 at 07:17

2 Answers2

1

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];
        }
    }
Ashvin
  • 8,227
  • 3
  • 36
  • 53
0

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.

Diziet
  • 2,397
  • 1
  • 26
  • 34
  • Thanks for Your answer but code given by you is already implemented by my app..i need the java script or JQuery code to put in there which works for almost any web page.. – Manish Verma Apr 05 '12 at 07:32
  • 1
    During developing my app i have come across various web pages which only allow to autofill the username field and leaves the password field empty. For ex: login page of mail.google.com autofills only username field. – Manish Verma Apr 05 '12 at 07:37
  • Now what Javascript/ JQuery code i should use to autofill the web page ? – Manish Verma Apr 05 '12 at 08:29
  • As I said, it's website dependent. So I can't really answer that without knowing the site and whether or not it uses jquery already. Also, you probably don't want to use any javascript I write. I'm a total novice at the language. }:) Hopefully someone can weigh in with advice there, if you edit the question to make it clearer that you need JS advice. – Diziet Apr 05 '12 at 14:45