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....
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...