title: How do I autofill username and password in UIWebView iOS app?
body: <p>I am teaching myself how to program an iPhone app by looking at code examples from various sources online, so it is fair to say that I do not understand the language (yet).  </p>
  
  <p>I am have successfully built a UIWebView browser app that goes to a login page.  However, I am trying to take it one step further by having the </p>
  
  <p>Following Byron's code on his own stack overflow question, I have tried to follow in his footsteps.  </p>
  
  <p><a href="https://stackoverflow.com/questions/4772341/is-it-possible-for-a-uiwebview-to-save-and-autofill-previously-entered-form-valu">Is it possible for a UIWebView to save and autofill previously entered form values (e.g., username &amp; password)?</a></p>
  
  <p>However, when the following line of code is live in the app, the browser will only load a blank page. </p>
  
  <pre><code>    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {</code></pre>
  
  <p>I really appreciate any assistance to help get me back on the right track.  Thank you very much, </p>
  
  <p>Below is my entire code:</p>
  
  <pre><code>
  
      #import "ViewController.h"
  
      // 6/13/2012 added to cache username and password
      #import 
      #import "SFHFKeychainUtils.h"
       // -----
  
  
      @interface ViewController ()
  
      @end
  
  
      @implementation ViewController
      @synthesize webView;
      @synthesize spinner;
  
      -(IBAction)goBack:(id)sender{
          if ([webView canGoBack]) {
              [webView goBack];
          }
      }
      -(IBAction)goForward:(id)sender{
          if ([webView canGoForward]){
              [webView goForward];
          }
  
      }
  
  
      - (void)viewDidLoad
      {
          NSURL *url = [NSURL URLWithString:@"http://xxxxxx.com/weblink/hh_login.html"];
          NSURLRequest *request = [NSURLRequest requestWithURL:url];
          [webView loadRequest:request];
          [super viewDidLoad];
          // Do any additional setup after loading the view, typically from a nib.
  
      }
  
      // 6/13/2012 added to cache username and password
      - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request        navigationType:(UIWebViewNavigationType)navigationType; {
  
          //save form data
          if(navigationType == UIWebViewNavigationTypeFormSubmitted) {
  
              //grab the data from the page
              NSString *username = [self.webView stringByEvaluatingJavaScriptFromString: @"document.myForm.username.value"];
              NSString *password = [self.webView stringByEvaluatingJavaScriptFromString: @"document.myForm.password.value"];
  
              //store values locally
              [[NSUserDefaults standardUserDefaults] setObject:username forKey:@"username"];
              [SFHFKeychainUtils storeUsername:username andPassword:password     forServiceName:@"MyService" updateExisting:YES error:nil];
  
          }    
  
      }
      // -----
  
  
      - (void)viewDidUnload
      {
          [super viewDidUnload];
          // Release any retained subviews of the main view.
      }
  
      - (BOOL)shouldAutorotateToInterfaceOrientation:         (UIInterfaceOrientation)interfaceOrientation
      {
          return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
      }
  
      - (void)webViewDidStartLoad:(UIWebView *)webView {
          [spinner startAnimating];
      }
  
      //- (void)webViewDidFinishLoad:(UIWebView *)webView {
      //    [spinner stopAnimating];
      //}
  
      // 6/13/2012 added to cache username and password
      - (void)webViewDidFinishLoad:(UIWebView *)webView{
          [spinner stopAnimating];
  
          //verify view is on the login page of the site (simplified)
          NSURL *requestURL = [self.webView.request URL];
          if ([requestURL.host isEqualToString:@"http://xxxxxx.com/weblink/hh_login.html"]) {
  
              //check for stored login credentials
              NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
  
              if (username.length != 0 ) {
  
                  //create js strings
                  NSString *loadUsernameJS = [NSString     stringWithFormat:@"document.myForm.username.value ='%@'", username];
                  NSString *password = [SFHFKeychainUtils getPasswordForUsername: username andServiceName:@"MyService" error:nil];
                  if (password.length == 0 ) password = @"";
                  NSString *loadPasswordJS = [NSString stringWithFormat:@"document.myForm.password.value ='%@'", password];
  
                  //autofill the form
                  [self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
                  [self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
  
              }
          }   
      }
      // -----
  
      @end
  
  </code></pre>