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

Posted

技术标签:

【中文标题】从 UIAlertview 获取用户名、密码以在 willSendRequestForAuthenticationChallenge xcode 中使用【英文标题】:Getting username,password from UIAlertview to use in willSendRequestForAuthenticationChallenge xcode 【发布时间】:2013-03-13 04:31:11 【问题描述】:

我正在尝试从 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。此外,如果我在 didDismissWithButtonIndex 中进行挑战,我也会出错。 我想知道怎么做。请帮助我。

【问题讨论】:

【参考方案1】:

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

如果这不是我推荐的场景,请详细说明您到底想要什么。

【讨论】:

谢谢兄弟。我找到了办法。【参考方案2】:

哦....我想我找到了一种方法。我记得 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];
    

【讨论】:

以上是关于从 UIAlertview 获取用户名、密码以在 willSendRequestForAuthenticationChallenge xcode 中使用的主要内容,如果未能解决你的问题,请参考以下文章

如何从 UIAlertView 获取取消和调用用户操作

如何自定义 UIAlertView?苹果会批准吗?

防止 UIAlertView 关闭

UIAlertView 与 UIActivityIndi​​cator 有点矛盾

UIAlertView 输入 Apple ID 和密码 IAP

iOS 7 中的 UIAlertView 样式登录和密码输入