如何让 UIWebView 专注于表单输入并调出键盘?

发布于 2024-10-07 10:36:37 字数 652 浏览 14 评论 0原文

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

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

唉,这不起作用。奇怪的是,当我在 Firefox JavaScript 控制台中运行该 JavaScript 时,它确实将焦点移至电子邮件字段。然而,当我在 Mac 版 Safari 中运行它时,它什么也没做,就像在 iOS 上一样。 FWIW,alert(document.getElementsByName('email')[0]) 确实表明它选择了一个 HTMLInputElement。那么为什么 focus() 在 iOS 或 Mac OS X 中什么都不做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舞袖。长 2024-10-14 10:36:37

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];

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];
江心雾 2024-10-14 10:36:37

正如 soumya92 在原始帖子的评论中指出的那样,这篇文章发现目前还不行。唉。

As soumya92 points out in the comment to the original post, this post finds that it's not possible at this time. Alas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文