自动填充UIWebView

时间:2013-05-16 05:20:49

标签: javascript ios uiwebview autofill

我一直在四处寻找,并找到了很多自动填充的代码,如下所示:

 - (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];
}
}

这就是我在.m中所拥有的,然而,当我转到webView时,字段仍为空白。关于它为什么不能填补的任何线索?

如果有帮助,我想填写的网站是:sis.ocsarts.net,相关代码是:

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

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

我的FirstViewController.m的完整代码是:

//
//  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

1 个答案:

答案 0 :(得分:0)

您尝试访问的网站http://sis.ocsarts.net包含http://sis.ocsarts.net/login.asp的框架。

更改Web视图以从该login.asp页面加载。

我通过使用Chrome开发者控制台发现它,尝试在您提到的原始URL上执行javascript,只是为了发现javascript函数以获取输入元素始终返回空数组。我甚至试过了getElementsByName,getElementsByTagName等等。

确保链接webview委托,并尝试将javascript字符串合并为一个:

NSString *loadUsernameAndPasswordJS = [NSString stringWithFormat:@"document.getElementsByTagName('input')[1].value = '%@';document.getElementsByTagName('input')[2].value = '%@';", savedUsername, savedPassword];