1

i try to access a protected webfolder with the webview. with "hard coded" user and pass it works, but my plan is to pop up an alertview to enter user and pass. here is the part of code:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:    (NSURLAuthenticationChallenge *)challenge{

NSLog(@"Need Authentication");

UIAlertView *webLogin = [[UIAlertView alloc] initWithTitle:@"Authentication" 
                                                   message:@"Enter User and Pass" 
                                                   delegate:self 
                                                   cancelButtonTitle:@"Cancel" 
                                                   otherButtonTitles:@"OK"
                                                   , nil];


webLogin.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[webLogin show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

user = [[alertView textFieldAtIndex:0]text];
pass = [[alertView textFieldAtIndex:1]text];
NSLog(@"user is %@ and pass is %@",user,pass);

if (buttonIndex == [alertView cancelButtonIndex]) {

    [self dismissModalViewControllerAnimated:YES];
}
else if (buttonIndex != [alertView cancelButtonIndex]) {

    NSLog(@"OK Pressed");
    [self handleAuthentificationOKForChallenge:nil withUser:user password:pass];
}


}

- (void)handleAuthentificationOKForChallenge:(NSURLAuthenticationChallenge *)aChallenge     withUser:(NSString *)userName password:(NSString *)password {

NSURLCredential *credential = [[NSURLCredential alloc]
                               initWithUser:userName password:password
                               persistence:NSURLCredentialPersistenceForSession];
[[aChallenge sender] useCredential:credential forAuthenticationChallenge:aChallenge];

}

can anybody tell me how to call the handleAuthenticationOKForChallenge i´m a little bit confused with the NSURLAuthenticationChallenge....

HugoBoss
  • 95
  • 3
  • 11

1 Answers1

1

First things first, you shouldn't use two if statements one after the other if they're comparing the same variable. Your second if statement should be an else if statement.

It looks like your handleAuthentificationOKForChallenge method wants to accept an instance of NSURLAuthenticationChallenge, but you're currently just passing it nil.

Why don't you declare an instance of NSURLAuthenticationChallenge in your header file (let's call it myChallenge), and in your first method, allocate and initialize it with challenge. You could also just set it equal to challenge (might work, if you feel like trying this first), but you may lose the pointer at some point. Then you would change your line in your second method to:

[self handleAuthentificationOKForChallenge:myChallenge withUser:user password:pass];

Let me know if this works...

Dylan Reich
  • 1,400
  • 2
  • 11
  • 14
  • else if corrected :-) so, can you tell me how to initialize the ivar? myChallenge = [[NSURLAuthenticationChallenge alloc] initWith ...? – HugoBoss Jun 29 '12 at 13:23
  • Try: myChallenge = [[NSURLAuthenticationChallenge alloc] initWithAuthenticationChallenge:challenge sender:challenge.sender]; – Dylan Reich Jun 29 '12 at 15:15
  • 1
    dont work! is it okay to have different values of challenge and mychallenge? – HugoBoss Jun 29 '12 at 17:05
  • How can challenge and mychallenge be different if you are initializing mychallenge from challenge? And what error are you getting? – Dylan Reich Jun 29 '12 at 17:23
  • NSLog(@"%@",challenge) is 0x6b9ab50, and NSLog(@"%@",myChallenge) is 0x8199ef0; the error is, after entering user and pass nothing happens.. – HugoBoss Jun 29 '12 at 17:31
  • Yes, that's normal because they're different objects. Is your third method getting called? Set a breakpoint in the third method to see. – Dylan Reich Jun 29 '12 at 17:41
  • Double check that user and password have data. If they're fine, try just setting a pointer for myChallenge. myChallenge = challenge; – Dylan Reich Jun 29 '12 at 17:56
  • 1
    got it! the [webView loadRequest:req]; was missing in my method. to init the ivar myChallenge = challenge is absolutely correct! thx4 your help dylan!! – HugoBoss Jun 29 '12 at 18:23