To make this clear I'm going to divide my question.
Goal:
After executing some Javascript check the a element on the page ONCE; but if it's incorrect I want to be able to recheck it on the press of a button.
What I've tried:
-(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;
}
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");
gets printed out when it shouldn't and failedLogin is equal to @"Failed Login". If I remove the else statement it always verifies the if statement as false login credentials when true, that's why I think I'm checking it before I should before it is processed.
My guess:
I'm running this if statement when the webView hasn't finished executing the javascript and/or reloading the webpage after button click.
Resources:
I don't know how this can help but the site I'm working with is: tapgram.com/login
The full current code is available here: http://pastebin.com/dnCn62FP For full projet code please comment and I'll add the link.