【问题标题】:Adding text to a UIWebView text box向 UIWebView 文本框添加文本
【发布时间】:2011-10-20 18:09:01
【问题描述】:

如何在我的 iOS 应用程序中将 UITextField 中的一些文本以物理方式放入 UIWebView 的表单中?也许可以调用一个javascript字符串来输入文本?

谢谢!

【问题讨论】:

标签: iphone objective-c ios xcode uiwebview


【解决方案1】:

就像 Matt H 建议的那样,您需要注入您的 javascript。所以这个问题基本上有两个部分。第一,你有你的目标 C。

    NSString *username = @"Fred";
    NSString *password = @"pAssw0rd"; 
    NSString *jsCallBack = [NSString stringWithFormat:@"updateWebForm('%@,%@');",username,password];    
    [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];

然后你将拥有你的 javascript 函数,我把我的放在一个 .js 文件中,我的 webview 页面链接到该文件。

updateWebForm(username,password)
{
 //here is where you will assign whatever html/javascript needed for your form.
}

【讨论】:

  • 谢谢,好东西,但我不明白我需要在 updateWebForm javascript 部分添加什么:S
  • 要更新它,将用户名、密码更改为您的 username.text、password.text。 javascript函数将包含.. document.getElementById('usernamefield').value = username; document.getElementById('passwordfield').value = 密码;
  • 太好了,谢谢! :D 我现在如何在 XCode 中链接 .js 文件? :)
  • 它似乎对我不起作用,文本框仍然是空的。这是我正在使用的代码:pastie.org/2731306 - 怎么了?
  • hmm...我的 javascript 并不出色。但是在javascript中你说的是username.text。这可能有效,但我很确定你只是传递一个字符串。它不是对象或任何东西。也许只尝试“用户名”。在你的 java 函数中。
【解决方案2】:

您可以将 Javascript 注入 UIWebView:

[webView stringByEvaluatingJavaScriptFromString:@"javascript goes here"];

与 $('#fieldid').val('fromuitextfield'); 一起使用或使用 document.getElementById() 或者您需要更改文本字段值属性。

【讨论】:

  • 我怎样才能输入 username.text 和 password.text 而不是 ID usernamefield 和 passwordfield?你能举个例子吗? :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多