以编程方式填充UIWebview中的数据。

时间:2022-08-24 23:26:21

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. I want the UIWebView to be programmatically filled in with the user's username and password. Then the UIWebView should programmatically click the login button. I've been trying:

在我的应用程序中,我希望能够让用户在自定义视图中输入他们的登录信息,然后显示一个UIWebView。我希望用户的用户名和密码以编程方式填充UIWebView。然后,UIWebView应该以编程方式点击登录按钮。我一直在尝试:

[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';"];

2 个解决方案

#1


5  

That method takes JavaScript, executes it in the context of the document, then returns the result to you. It appears your JavaScript is wrong. Try:

该方法使用JavaScript,在文档的上下文中执行它,然后将结果返回给您。看起来你的JavaScript是错误的。试一试:

[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引用库,找到您正在使用的类的类引用,并向下滚动到您正在使用的方法的描述。

#2


3  

Bil, I was able to get mine working with the below. Essentially, the element with Id of 'logon-field' is just that. 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. 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.

Bil,我能让我的工作在下面。本质上,Id为'log -field'的元素就是这个。我将JavaScript分解成多行,以便插入我的变量(来自UITextField在我的自定义屏幕上的用户输入值,在我的UIWebView上)并连接。我将下面的代码放在我的textFieldShouldReturn:方法中的视图控制器中,这样直到用户在文本字段中输入完毕才会运行。

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

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

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

[webView stringByEvaluatingJavaScriptFromString:logonJSFinal];

#1


5  

That method takes JavaScript, executes it in the context of the document, then returns the result to you. It appears your JavaScript is wrong. Try:

该方法使用JavaScript,在文档的上下文中执行它,然后将结果返回给您。看起来你的JavaScript是错误的。试一试:

[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引用库,找到您正在使用的类的类引用,并向下滚动到您正在使用的方法的描述。

#2


3  

Bil, I was able to get mine working with the below. Essentially, the element with Id of 'logon-field' is just that. 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. 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.

Bil,我能让我的工作在下面。本质上,Id为'log -field'的元素就是这个。我将JavaScript分解成多行,以便插入我的变量(来自UITextField在我的自定义屏幕上的用户输入值,在我的UIWebView上)并连接。我将下面的代码放在我的textFieldShouldReturn:方法中的视图控制器中,这样直到用户在文本字段中输入完毕才会运行。

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

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

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

[webView stringByEvaluatingJavaScriptFromString:logonJSFinal];