2013-03-13 71 views
2

我想從uialertview獲取用戶名和密碼並傳遞給willSendRequestForAuthenticationChallenge。這些是我的代碼。從UIAlertview獲取用戶名和密碼以用於willSendRequestForAuthenticationChallenge xcode

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login" 
                message:nil 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK",@"Auto", nil]; 

alertView.transform=CGAffineTransformMakeScale(1.0, 0.75); 
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
[alertView show]; 


if ([challenge previousFailureCount] == 0) 
    { 
     NSLog(@"Inside challenge previousFailureCount==0"); 
     NSURLCredential *credentail = [NSURLCredential 
             credentialWithUser:Username 
             password:Password 
             persistence:NSURLCredentialPersistenceNone]; 


     [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge]; 
    } 
} 
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
//Index1 = OK 
//Index2 = Auto 
//Index0 = Cancel 

NSLog(@"Alert View dismissed with button at index %d",buttonIndex); 

if(buttonIndex==1) 
{ 
    NSURLAuthenticationChallenge *challenge; 
    Username= [alertView textFieldAtIndex:0].text; 

    Password= [alertView textFieldAtIndex:1].text; 

} 
} 


的問題是我無法從UIAlertView中調用用戶名和密碼pass.If我寫上面的代碼,用戶寫入的用戶名和密碼之前,它會做的挑戰。我們必須等到用戶按OK。而且,如果我在didDismissWithButtonIndex中挑戰,我也會遇到錯誤。 我想知道如何做。請幫助我。

回答

0

while click on alertView button it dismiss alertView .Better take on more alertView and pass your value there。

如果這不是塞納里奧,我會建議請詳細說明你真正想要的。

+0

感謝兄弟。我找到了一個辦法。 – 2013-03-13 06:45:33

0

哦....我想我找到了一個辦法。我可以回想起willSendRequestForAuthenticationChallenge.So,首先,UIalertview將顯示。我們將保存用戶名和密碼。然後,我們回想一下willSendRequestForAuthenticationChallenge並傳遞用戶名和密碼。

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    //Index1 = OK 
    //Index2 = Auto 
    //Index0 = Cancel 
    checkUserPw=TRUE; 
    checkTochangeUIalert=FALSE; 
    NSLog(@"Alert View dismissed with button at index %d",buttonIndex); 

    if(buttonIndex==1) 
    { 
     checkManualAuthentication=TRUE; 
     NSLog(@"User clicks OK"); 
     self.UserName = [alertView textFieldAtIndex:0].text; 

     self.Password = [alertView textFieldAtIndex:1].text; 

     connection_for_auto = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL:toRedirectURL] delegate:self]; 

     [connection_for_auto start]; 

    } 
} 


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

if(!checkUserPw){ 
    //so that alert will display only once. 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login" 
                message:nil 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK",@"Auto", nil]; 

    alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; 
    [alertView show]; 
    } 
    else 
    { 
     NSURLCredential *credentail =nil; 
     credentail = [NSURLCredential 
             credentialWithUser:self.UserName 
             password:self.Password 
             persistence:NSURLCredentialPersistenceNone]; 
    [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge]; 
    } 
}