To understand this, I am going to share my question.
Purpose:
After doing some Javascript, check the element on the ONCE page ; but if this is not correct, I want to check it by pressing a button.
What I tried:
-(IBAction)signin {
[passwordTF resignFirstResponder];
[usernameTF resignFirstResponder];
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];
[mainWebView stringByEvaluatingJavaScriptFromString:loadUsernameJS];
[mainWebView stringByEvaluatingJavaScriptFromString:loadPasswordJS];
[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;
}
Problem:
For some reason here:
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");
}
The if statement always returns false and
NSLog(@"webViewDidiFinishLoad - shouldVerify YES - success");
prints when it should not, and failedLogin is @ "Failed Login". If I delete the else statement, it always checks the if statement as a false login credential when it is true, so I think I will check it before I have it before it is processed.
My suggestion:
if, webView javascript / - .
, , , , : tapgram.com/login
: http://pastebin.com/dnCn62FP
, .