我尝试使用用户名和密码对用户进行身份验证但是在didReceiveAuthenticationChallenge
方法中,即使我输入了错误的用户名或密码,它也不会转到else
。这是我的代码:
- (IBAction)clickOK:(id)sender {
if ([[usernameField text] length] < 1 || [[passwordField text] length] < 1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Neither the username nor password may be empty!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
NSURL *myURL = [NSURL URLWithString:@"https://www.freelancer.com/users/login.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
NSLog(@"ispost");
[request setHTTPMethod:@"POST"];
NSString* str = [NSString stringWithFormat:@"%@:%@", usernameField.text, passwordField.text];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!theConnection) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Could not login!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
return YES;
}
- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:usernameField.text
password:passwordField.text
persistence:NSURLCredentialPersistenceNone];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@"Invalid Username or Password");
UIAlertView *userNameAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Invalid credentials. Please check your login details !" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
[userNameAlert show];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Got response: %@", response);
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
return;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];
// Show error message
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Use responseData
NSLog(@"Succeeded! Received %d bytes of data",[responseData length]);
[responseData release];
[connection release];
}
答案 0 :(得分:1)
更新didReceiveAuthenticationChallenge方法,如下所示:
- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:usernameField.text
password:passwordField.text
persistence:NSURLCredentialPersistenceNone];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
} else {
NSLog(@"previous authentication failure");
UIAlertView *userNameAlert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Invalid credentials. Please check your login details !" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
[userNameAlert show];
}
和clickOk方法:
- (IBAction)clickOK:(id)sender {
if ([[usernameField text] length] < 1 || [[passwordField text] length] < 1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Neither the username nor password may be empty!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
NSURL *myURL = [NSURL URLWithString:@"http://test.webdav.org/auth-basic/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[theConnection start];
}
}