简体   繁体   中英

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. 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:

[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.

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. It appears your JavaScript is wrong. 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 .

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.

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

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

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

[webView stringByEvaluatingJavaScriptFromString:logonJSFinal];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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