UI inside auth delegate

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

  1. chhoda macrumors 6502

    Joined:
    Oct 25, 2008
    #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
     
  2. chhoda thread starter macrumors 6502

    Joined:
    Oct 25, 2008
    #2
    none tried ?

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

    Joined:
    Oct 25, 2008
    #3
    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
     
  4. teek macrumors member

    teek

    Joined:
    Feb 12, 2008
    Location:
    Norway
    #4
    Dont you have to call
    connectionNSURLConnection *)connection didReceiveAuthenticationChallengeNSURLAuthenticationChallenge *)challenge

    again after getting the username, password ?
     

Share This Page