爲了清楚起見,我將分解我的問題。在自動填充後在UIWebView中執行Javascript後驗證標籤的長度
目標:
執行一些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");
當它不應該和失敗登錄等於@「失敗登錄」時被打印出來。如果我刪除了else語句,它總是會驗證if語句是否爲false登錄憑據,這就是爲什麼我認爲在應該處理它之前檢查它的原因。
我的猜測:
我if語句時webView的尚未執行完畢的JavaScript和/或重裝按鈕,點擊後網頁運行此。
資源:
我不知道如何可以幫助,但我與工作的網站:tapgram.com/login
完整當前的代碼可以在這裏找到:http://pastebin.com/dnCn62FP 對於完整的projet代碼請評論,我會添加鏈接。
failedLogin的值是多少? – radical
「登錄」的情況不匹配。您正在比較「失敗登錄」和「失敗登錄」。你可以使用caseInsensitiveCompare:也。 – radical
它應該是失敗的登錄我輸錯了對不起。 – ge0rges