简体   繁体   English

以编程方式填写UIWebview中的数据

[英]Programmatically fill in data in a UIWebview

In my app, I want to be able to have the user enter their login info in a custom view and then show a UIWebView. 在我的应用程序中,我希望能够让用户在自定义视图中输入他们的登录信息,然后显示UIWebView。 I want the UIWebView to be programmatically filled in with the user's username and password. 我希望UIWebView以编程方式填入用户的用户名和密码。 Then the UIWebView should programmatically click the login button. 然后UIWebView应以编程方式单击登录按钮。 I've been trying: 我一直在努力:

[webView stringByEvaluatingJavaScriptFromString:@"document.L_lbsc_txtUserName.text.value='ANdrew';"];

but its not working. 但它不起作用。 I don't really understand how stringByEvaluatingJavaScriptFromString works or what it's designed to do. 我真的不明白stringByEvaluatingJavaScriptFromString如何工作或它的设计目的。

I think it might have something to do with what I filled in the string field above. 我认为这可能与我在上面的字符串字段填写的内容有关。 Here is the source code of the website I'm trying to access: 这是我试图访问的网站的源代码:

<div class="lgntxt">
  <div style="padding-left:3px;">
    <input name="L$lbsc$txtUserName" type="text" value=" -username-" size="10" id="L_lbsc_txtUserName" tabindex="1" class="textbox" onfocus="if(this.value == ' -username-') this.value = ''; Hide('L_lbsc_txtPasswordHelp'); Show('L_lbsc_txtPassword');" onblur="if(this.value == '') this.value = ' -username-';" style="width:80px;"><br>
    <img src="/podium/images/spacer.gif" border="0" width="3" height="1"><br>
    <input name="L$lbsc$txtPasswordHelp" type="text" value=" -password-" size="10" id="L_lbsc_txtPasswordHelp" tabindex="1" class="textbox" onfocus="Hide('L_lbsc_txtPasswordHelp'); Show('L_lbsc_txtPassword'); document.getElementById('L_lbsc_txtPassword').focus();" style="width:80px;display:;">
    <input name="L$lbsc$txtPassword" type="password" size="10" id="L_lbsc_txtPassword" tabindex="2" class="textbox" onkeydown="if ((event.which ? event.which : event.keyCode) == 13) {return false;};" onkeyup="if ((event.which ? event.which : event.keyCode) == 13) {__doPostBack('L$lbsc$btnLogin','');return false;};" onblur="if(this.value == '') {Show('L_lbsc_txtPasswordHelp'); Hide('L_lbsc_txtPassword');}" style="width:80px;display:none;">
  </div>
</div>

I'm trying to replace the username and password fields. 我正在尝试替换用户名和密码字段。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

EDIT so I've tried these and none of them work: 编辑,所以我尝试了这些,但没有一个工作:

[webView stringByEvaluatingJavaScriptFromString:@"document.myForm.myText.value='text from Obj-C';"];

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('L$lbsc$txtUserName').value='ANdrew';"];

That method takes JavaScript, executes it in the context of the document, then returns the result to you. 该方法接受JavaScript,在文档的上下文中执行它,然后将结果返回给您。 It appears your JavaScript is wrong. 看来你的JavaScript是错的。 Try: 尝试:

[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('L$lbsc$txtUserName').value='ANdrew';"];

If you are unsure what a method does, go to the iOS reference library , find the class reference for the class you are using , and scroll down to the description of the method you are using . 如果您不确定方法的作用,请转到iOS参考库 ,找到您正在使用的类的类引用 ,然后向下滚动到您正在使用的方法的描述

Bil, I was able to get mine working with the below. Bil,我能够让我的工作与下面的工作。 Essentially, the element with Id of 'logon-field' is just that. 基本上,ID为'logon-field'的元素就是这样。 I broke the JavaScript up into multiple lines so that I could insert my variable (coming from the UITextField's user-entered value in my custom screen, on top of my UIWebView ) and concatenate. 我将JavaScript分成多行,以便我可以插入我的变量(来自我的自定义屏幕中的UITextField's用户输入值,在我的UIWebView之上)并连接。 I put the below code in my textFieldShouldReturn : method in the view controller, so that this isn't run until the user is done entering in the text field. 我将以下代码放在视图控制器中的textFieldShouldReturn :方法中,以便在用户输入文本字段之前不会运行此代码。

NSString *logonJSStart = @"document.getElementById('logon-field').value='";

NSString *logonWithValue = [logonJSStart stringByAppendingString:self.logon.text];

NSString *logonJSFinal = [logonWithValue stringByAppendingString:@"';"]; 

[webView stringByEvaluatingJavaScriptFromString:logonJSFinal];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM