Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chhoda

macrumors 6502
Original poster
Oct 25, 2008
285
1
I am using
Code:
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

function to pass through my firrewall using my usercedentials hard coded here.

But I want user to be prompted for username and password through an UI

but when i show the UI, it is non blocking and the response receipt is not waiting for the username and password entered. Any idea how to achieve this ?

--CH
 
not working

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

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Who are you?"
message:mad:"Authenticate"
delegate:self cancelButtonTitle:mad:"Cancel"
otherButtonTitles:mad:"OK", nil];
[alert addTextFieldWithValue:mad:""label:mad:"Username"];
[alert addTextFieldWithValue:mad:""label:mad:"Password"];

// Customise name field
UITextField* name = [alert textFieldAtIndex:0];
name.clearButtonMode = UITextFieldViewModeWhileEditing;
name.keyboardType = UIKeyboardTypeAlphabet;
name.keyboardAppearance = UIKeyboardAppearanceAlert;
[alert show];

[connection cancel];
return;
}

if([challenge previousFailureCount]==0)
{
NSURLCredential *userCredential = [NSURLCredential credentialWithUser:userName password:password persistence:NSURLCredentialPersistenceForSession];

[[challenge sender] useCredential: userCredential forAuthenticationChallenge: challenge];
}
else
{
[[challenge sender] cancelAuthenticationChallenge: challenge];
NSLog(@"Auth Failed");
}
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { // after animation

if(1 == buttonIndex) // OK button presed
{
UITextField* name = [alertView textFieldAtIndex:0];
UITextField* pswd = [alertView textFieldAtIndex:1];
userName = [name text];
password = [pswd text];
bAuthenticated = YES;
[conn start];
}
}


__________________

I am trying this code, but connection is not again sending the request !

--CH
 
Dont you have to call
connectionNSURLConnection *)connection didReceiveAuthenticationChallengeNSURLAuthenticationChallenge *)challenge

again after getting the username, password ?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.