我的应用程序有以下代码。
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Set the values
NSString* firstValue = @"guest";
NSString* secondValue = @"guest";
// write javascript code in a string
NSString* javaScriptString = @"document.getElementById('Login1_UserName').value=%@;"
"document.getElementById('Login1_Password').value=%@;"
"document.forms['form1'].submit();";
// insert string values into javascript
javaScriptString = [NSString stringWithFormat: javaScriptString, firstValue, secondValue];
// run javascript in webview:
[webView stringByEvaluatingJavaScriptFromString: javaScriptString];
}
- (void)launchWebPageWithOptions
{
NSString *urlString = @"http://demo.leadtools.com/MedicalViewer/default.aspx";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[iOSWebView loadRequest:requestObject];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self launchWebPageWithOptions];
}
由于某种原因,我无法使用javascript调用执行自动登录。我不是JS专家,所以如果你能在我的javaScriptString中看到任何错误,请告诉我,
答案 0 :(得分:1)
首先,更改要引用的用户和密码格式化值:
NSString* javaScriptString = @"document.getElementById('Login1_UserName').value='%@';"
"document.getElementById('Login1_Password').value='%@';"
"document.forms['form1'].submit();";
请注意,我在两种情况下都将value=%@
更改为value='%@'
。这将正确填写输入框。
接下来,在“登录”按钮上调用click()
函数,而不是在表单上调用submit()
:
NSString* javaScriptString = @"document.getElementById('Login1_UserName').value='%@';"
"document.getElementById('Login1_Password').value='%@';"
"document.getElementById('Login1_LoginButton').click()";
这将允许执行为Login按钮指定的onclick
javascript。
答案 1 :(得分:0)
您缺少字符串引号。
试试这个:
NSString* javaScriptString = @"document.getElementById('Login1_UserName').value='%@';"
"document.getElementById('Login1_Password').value='%@';"
"document.forms['form1'].submit();";