Question

I've been looking around, and have found lot of code for autofilling that looks like:

 - (void)webViewDidFinishLoad:(UIWebView *)webView
{
//For Authentication
NSString *savedUsername = @"Bapu";
NSString *savedPassword = @"Bapu123";

if (savedUsername.length != 0 && savedPassword.length != 0)
{
    //create js strings
    NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
    NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='password']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];

    //autofill the form
    [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}

And that's what I have in my .m, however, when I go to the webView, the fields remain blank. Any clues as to why it cannot fill?

If it helps, the website I am trying to fill is: sis.ocsarts.net, and the relevant code is:

input type="text" size="20" name="UserName" value=""  onkeydown="if (event.keyCode==13) { document.frmLogin.submit(); return false; };"

And

input type="password" size="20" name="Password"  onkeydown="if (event.keyCode==13) { document.frmLogin.submit(); return false; };"

The full code for my FirstViewController.m is:

//
//  FirstViewController.m
//  OCSA
//
//  Created by Jack Kearl on 4/1/13.
//  Copyright (c) 2013 Jack Kearl. All rights reserved.
//

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize Aeries;

- (void)viewDidLoad
  {
  [super viewDidLoad];
  [Aeries loadRequest:[NSURLRequest requestWithURL:
  [NSURL URLWithString:@"http://sis.ocsarts.net/login.asp"]]];

  }

 - (void)webViewDidFinishLoad:(UIWebView *)webView
 {
//For Authentication
NSString *savedUsername = @"Bapu";
NSString *savedPassword = @"Bapu123";

if (savedUsername.length != 0 && savedPassword.length != 0)
{
    //create js strings
    NSString *loadUsernameJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='text']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedUsername];
    NSString *loadPasswordJS = [NSString stringWithFormat:@"var inputFields = document.querySelectorAll(\"input[type='password']\"); \
                                for (var i = inputFields.length >>> 0; i--;) { inputFields[i].value = '%@';}", savedPassword];

    //autofill the form
    [webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
    [webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
Était-ce utile?

La solution

The website you try to access http://sis.ocsarts.net contains frame to http://sis.ocsarts.net/login.asp.

Change the web view to load from that login.asp page instead.

I discovered it by using Chrome developer console, trying to execute the javascript on the original URL you mentioned, just to discover the javascript function to get the input element always returns empty array. I even tried out getElementsByName, getElementsByTagName, etc.

Make sure the webview delegate is linked and try to combine the javascript string into one:

NSString *loadUsernameAndPasswordJS = [NSString stringWithFormat:@"document.getElementsByTagName('input')[1].value = '%@';document.getElementsByTagName('input')[2].value = '%@';", savedUsername, savedPassword];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top