0

我尝试使用用户名和密码对用户进行身份验证但在该方法中,即使我输入了错误的用户名或密码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];
}
4

1 回答 1

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];
    }
 }
于 2012-07-08T14:22:07.083 回答