title: Is it possible for a UIWebView to save and autofill previously entered form values (e.g., username & password)?
body: <p>I'm building an iPhone app that is just a <code>UIWebView</code> of an existing mobile site that has a form-based login. When I login to the mobile site on iPhone Safari, I'm prompted to save my username/password, and it's then autofilled when I go back to the site later.</p>
  <p>I'd like to enable the same functionality in the <code>UIWebView</code>, but for the life of me, I can't figure out how to do it.  Any ideas?</p>
  <hr />
  <h1>Solution</h1>
  <p>Following Michael's basic model (see accepted answer), I was able to get this done.  Here's what I did:</p>
  <p>SETTING DATA</p>
  <pre><code>- (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: @&quot;document.myForm.username.value&quot;];
          NSString *password = [self.webView stringByEvaluatingJavaScriptFromString: @&quot;document.myForm.password.value&quot;];
      
          //store values locally
          [[NSUserDefaults standardUserDefaults] setObject:username forKey:@&quot;username&quot;];
          [SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:@&quot;MyService&quot; updateExisting:YES error:nil];
      
      }    
  
  }
  </code></pre>
  <p>GETTING DATA</p>
  <pre><code>- (void)webViewDidFinishLoad:(UIWebView *)webView{
      
      //verify view is on the login page of the site (simplified)
      NSURL *requestURL = [self.webView.request URL];
      if ([requestURL.host isEqualToString:@&quot;www.mydomain.com&quot;]) {
  
          //check for stored login credentials
          NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@&quot;username&quot;];
      
          if (username.length != 0 ) {
          
              //create js strings
              NSString *loadUsernameJS = [NSString stringWithFormat:@&quot;document.myForm.username.value ='%@'&quot;, username];
              NSString *password = [SFHFKeychainUtils getPasswordForUsername: username andServiceName:@&quot;MyService&quot; error:nil];
              if (password.length == 0 ) password = @&quot;&quot;;
              NSString *loadPasswordJS = [NSString stringWithFormat:@&quot;document.myForm.password.value ='%@'&quot;, password];
          
              //autofill the form
              [self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
              [self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
          
          }
      }   
  }
  </code></pre>
  <p>Note that I'm using Buzz Andersen's awesome <a href="https://github.com/ldandersen/scifihifi-iphone/tree/master/security" rel="noreferrer">SFHFKeychainUtils</a> package to store sensitive data to the iOs Keychain.</p>
  <p>In order to get SFHFKeychainUtils working, you need to do a few things:</p>
  <ol>
  <li>Add <code>SFHFKeychainUtils.h</code> and <code>SFHFKeychainUtils.m</code> to your project</li>
  <li>Add the <code>Security.framework</code> to your project</li>
  <li><code>#import &lt;Security/Security.h&gt;</code> and <code>#import &quot;SFHFKeychainUtils.h&quot;</code></li>
  </ol>