我正在编写一个询问用户名和密码的警报视图,但是,我想在从传入的 NSURLAuthenticationChallenge* 中使用这些凭据
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
有什么方法可以调用 UIAlertview 并将挑战变量临时保存在其中,以便以后在调用 UIAlertView 的委托方法时可以使用它?使用属性或实例变量似乎不优雅。
这是我的代码:
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"got authorization challange");
if ([challenge previousFailureCount] == 0) {
UIAlertView * loginAlert = [[UIAlertView alloc] initWithTitle:@"Login" message:@"Enter your Username and Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sign In", nil];
loginAlert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[loginAlert show];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
[[[UIAlertView alloc] initWithTitle:@"Invalid username/PW" message:@"Please try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([alertView.title isEqualToString:@"Login"] &&
buttonIndex == 1)
{
/*want to use the challenge variable here to log in the user over the secure connection*/
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:[alertView textFieldAtIndex:0].text password:[alertView textFieldAtIndex:1].text persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
}
}