Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在自动填充后在UIWebView中执行Javascript后验证标签的长度_Javascript_Ios_Uiwebview - Fatal编程技术网

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

在自动填充后在UIWebView中执行Javascript后验证标签的长度,javascript,ios,uiwebview,Javascript,Ios,Uiwebview,为了说明这一点,我将把我的问题分成两半 NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success"); 目标: -(IBAction)signin { [passwordTF resignFirstResponder]; [usernameTF resignFirstResponder]; //create js strings NSString *loadUsernameJS

为了说明这一点,我将把我的问题分成两半

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
目标:

        -(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;
}
  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
执行一些Javascript后,检查页面上的a元素一次;但如果它是不正确的,我希望能够重新检查它的按钮按下

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
我所尝试的:

        -(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;
}
  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
问题:

        -(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;
}
  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
出于某种原因:

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

        }
  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
if语句总是返回false和

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

在不应该打印时打印出来,failedLogin等于@“Failed Login”。如果我删除else语句,当它为true时,它总是将If语句验证为假登录凭据,这就是为什么我认为我在处理它之前先检查它

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
我猜:

        -(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;
}
  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
当webView还没有完成javascript的执行和/或在点击按钮后重新加载网页时,我运行这个if语句

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
资源:

        -(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;
}
  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
我不知道这有什么帮助,但我正在使用的网站是:tapgram.com/login

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
此处提供了完整的当前代码:
有关完整的projet代码,请发表评论,我将添加链接。

错误在JavaScript中

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
NSString *failedLogin = [mainWebView 
stringByEvaluatingJavaScriptFromString:@"document.getElementById('login-msg')"];
返回login msg元素的完整HTML字符串

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
所以基本上你是在比较
失败的登录
​登录失败​

  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
JavaScript应该如下所示
document.getElementById('login-msg')。innerText
,如果登录确实失败,它将返回失败登录

失败登录的值是什么?登录的大小写不匹配。您正在比较“登录失败”与“登录失败”。您可以使用caseInsensitiveCompare:也可以。它应该是Failed login我键入了它很抱歉。如果我删除else语句,它总是会在为true时将If语句验证为false login凭据,这就是为什么我认为在处理它之前我会先检查它。您可以对failedLogin进行NSLog吗?它会像我说的那样返回Failed login。如果我检查页面的URL是否更容易?(/login vs/loginfailed…(字符数)
  NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");