在自动填充后在UIWebView中执行Javascript后验证Label的长度

时间:2013-12-10 20:20:01

标签: javascript ios uiwebview

为了明确这一点,我将分开我的问题。

目标:

执行一些Javascript后,检查页面上的元素 ONCE ;但如果它不正确,我希望能够通过按下按钮重新检查它。

我尝试了什么:

        -(IBAction)signin {

    [passwordTF resignFirstResponder];
    [usernameTF resignFirstResponder];

    //create js strings
    NSString *loadUsernameJS = [NSString stringWithFormat:@"var field = document.getElementById('login-form-username'); field.value='%@';", username];

    NSString *loadPasswordJS = [NSString stringWithFormat:@"var field = document.getElementById('login-form-password'); field.value='%@';", password];

    //autofill the form
    [mainWebView stringByEvaluatingJavaScriptFromString:loadUsernameJS];
    [mainWebView stringByEvaluatingJavaScriptFromString:loadPasswordJS];

    //auto login
    [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('submit-login').click();"];


}

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSLog(@"webViewDidiFinishLoad called");

    if (shouldVerify == YES) {

        NSLog(@"webViewDidiFinishLoad - shouldVerify YES");

        NSString *failedLogin = [mainWebView stringByEvaluatingJavaScriptFromString: @"document.getElementById('login-msg')"];

        if ([failedLogin isEqualToString:@"Failed login"]) {

            NSLog(@"webViewDidiFinishLoad - shouldVerify YES - failed");

            UIAlertView *WCE = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"The username or password you entered is incorrect please try again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [WCE show];

            [keychainItem resetKeychainItem];

            password = [keychainItem objectForKey:(__bridge id)(kSecValueData)];
            username = [keychainItem objectForKey:(__bridge id)(kSecAttrAccount)];

            [usernameTF setText:username];
            [passwordTF setText:password];


        } else {

            NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");

            ViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];

            UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:viewController];

            navBar.navigationBarHidden = YES;

            [self presentViewController:navBar animated:YES completion:nil];
        }

        shouldVerify = NO;
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if (navigationType == UIWebViewNavigationTypeOther) {

        NSLog(@"shouldStartLoadWithRequest - shouldVerify YES");

        shouldVerify = YES;

    } else {

        NSLog(@"shouldStartLoadWithRequest - shouldVerify NO");

        shouldVerify = NO;

    }

    return YES;
}

问题:

出于某种原因:

if ([failedLogin isEqualToString:@"Failed login"]) {

            NSLog(@"webViewDidiFinishLoad - shouldVerify YES - failed");

            UIAlertView *WCE = [[UIAlertView alloc] initWithTitle:@"Wrong Credentials" message:@"The username or password you entered is incorrect please try again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [WCE show];


        } else {

            NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");

        }

if语句始终返回false和

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");

在不应该打印时打印出来,failLogin等于@“登录失败”。如果我删除了else语句,它总是将if语句验证为false登录凭据,这就是为什么我认为我在处理它之前应该检查它。

我猜:

当webView没有在按钮点击后执行javascript和/或重新加载网页时,我正在运行这个if语句。

资源:

我不知道这有什么用,但我正在使用的网站是:tapgram.com/login

此处提供完整的当前代码:http://pastebin.com/dnCn62FP 有关完整的项目代码,请发表评论,我将添加链接。

1 个答案:

答案 0 :(得分:0)

错误发生在JavaScript中。

NSString *failedLogin = [mainWebView 
stringByEvaluatingJavaScriptFromString:@"document.getElementById('login-msg')"];

返回 login-msg 元素的完整HTML字符串。

所以基本上你要将Failed login<h4 id=​"login-msg" style=​"color:​red;​">​Failed login​</h4>进行比较。

JavaScript应该如下所示 document.getElementById('login-msg').innerText,如果登录确实失败,将返回登录失败