title: How can I get a UIWebView to focus on a form input and bring up the keyboard?
body: <p>I've created a UIWebView for logging into Facebook's Graph API OAuth interface. This works great, but I have to tap the first input field in order for the keyboard to pop up. Ideally, the keyboard would appear as soon as the form appears. So I've been trying to figure out how to make the first form field get the focus from the <code>webViewDidFinishLoad:</code> UIWebViewDelegate method, like so:</p>
  
  <pre><code>- (void)webViewDidFinishLoad:(UIWebView *)wv {
      [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('email')[0].focus();"];
  }
  </code></pre>
  
  <p>Alas, this does not work. Curiously, when I run that JavaScript in the Firefox JavaScript console, it does move the focus to the email field. When I run it in Safari for the Mac, however, it does nothing, just like on iOS. FWIW, <code>alert(document.getElementsByName('email')[0])</code> <em>does</em> show that it selects an HTMLInputElement. So why does <code>focus()</code> do nothing either in iOS or Mac OS X?</p>