1

私のアプリケーションには次のコードがあります。

- (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 にエラーが見られる場合はお知らせください。

4

2 に答える 2

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()";

これによりonclick、ログイン ボタンに指定された JavaScript を実行できるようになります。

于 2012-06-18T05:26:34.173 に答える
0

文字列の引用符がありません。

これを試して:

NSString* javaScriptString = @"document.getElementById('Login1_UserName').value='%@';"
"document.getElementById('Login1_Password').value='%@';"
"document.forms['form1'].submit();";
于 2012-06-18T05:28:38.210 に答える