正在从UIAlertview获取用户名、密码以在willSendRequestForAuthenticationChallenge xcode中使用

正在从UIAlertview获取用户名、密码以在willSendRequestForAuthenticationChallenge xcode中使用,authentication,uialertview,Authentication,Uialertview,我正在尝试从uialertview获取用户名和密码,并将其传递给willSendRequestForAuthenticationChallenge。这是我的密码 - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { UIAlertView *alertView = [[UIAlert

我正在尝试从uialertview获取用户名和密码,并将其传递给willSendRequestForAuthenticationChallenge。这是我的密码

- (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调用用户名和密码来通过。如果我在用户写入用户名和密码之前写入上述代码,它将执行挑战。我们必须等到用户按OK。此外,如果我用ButtonIndex在DidDismiss中进行挑战,我也会得到错误。
我想知道怎么做。请帮助我

单击alertView按钮时,它会关闭alertView。最好使用更多的alertView并将您的值传递到那里


如果这不是senario,我建议你详细说明你到底想要什么。

哦……我想我找到了一个方法。我还记得willSendRequestForAuthenticationChallenge。因此,首先,将显示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];
    }
}

谢谢兄弟。我找到了一个方法。