我怎样才能得到一个UIWebView专注于表单输入,并提出了键盘? [英] How can I get a UIWebView to focus on a form input and bring up the keyboard?

查看:173
本文介绍了我怎样才能得到一个UIWebView专注于表单输入,并提出了键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于登录到Facebook的Graph API OAuth界面的UIWebView。这很好,但我必须点击第一个输入字段才能弹出键盘。理想情况下,键盘会在窗体出现后立即出现。所以我一直想弄清楚如何使第一个表单字段从 webViewDidFinishLoad: UIWebViewDelegate方法获取焦点,如下所示:

   - (void)webViewDidFinishLoad:(UIWebView *)wv {
[webView stringByEvaluatingJavaScriptFromString:@document.getElementsByName('email')[0]。焦点();];
}

唉,这是行不通的。奇怪的是,当我在Firefox JavaScript控制台中运行这个JavaScript时,它将焦点移到了电子邮件字段。但是,当我在Safari上运行Mac时,它不会像iOS上一样。 FWIW, alert(document.getElementsByName('email')[0])显示它选择一个HTMLInputElement。那么为什么 focus()在iOS或Mac OS X中都不做任何事情?

解决方案
  UIWebView * webView =<#您的webView在这里#> 
[webView setKeyboardDisplayRequiresUserAction:NO];
[webView loadHTMLString:@< html>< head>< / head>< body>< form>< input id = \textField \/>< / form> ;<脚本>的document.getElementById(\ textField\)聚焦();< /脚本>< /体>< / HTML>中
baseURL:nil];


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 webViewDidFinishLoad: UIWebViewDelegate method, like so:

- (void)webViewDidFinishLoad:(UIWebView *)wv {
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('email')[0].focus();"];
}

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, alert(document.getElementsByName('email')[0]) does show that it selects an HTMLInputElement. So why does focus() do nothing either in iOS or Mac OS X?

解决方案

This is now possible with iOS 6.

UIWebView* webView = <#your webView here#>;
[webView setKeyboardDisplayRequiresUserAction:NO];
[webView loadHTMLString:@"<html><head></head><body><form><input id=\"textField\" /></form><script>document.getElementById(\"textField\").focus();</script></body></html>"
                baseURL:nil];

这篇关于我怎样才能得到一个UIWebView专注于表单输入,并提出了键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆