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