UI inside auth delegate

Discussion in 'iOS Programming' started by chhoda, Mar 17, 2010.

  1. chhoda macrumors 6502

    Oct 25, 2008
    I am using
    -(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 ?

  2. chhoda thread starter macrumors 6502

    Oct 25, 2008
    none tried ?

    no one tried it ? how is safari doing it ?
  3. chhoda thread starter macrumors 6502

    Oct 25, 2008
    not working

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

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Who are you?"
    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];

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

    [[challenge sender] useCredential: userCredential forAuthenticationChallenge: challenge];
    [[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 !

  4. teek macrumors member


    Feb 12, 2008
    Dont you have to call
    connectionNSURLConnection *)connection didReceiveAuthenticationChallengeNSURLAuthenticationChallenge *)challenge

    again after getting the username, password ?

Share This Page