我尝试使用 webview 访问受保护的 web 文件夹。使用“硬编码”用户并通过它可以工作,但我的计划是弹出一个警报视图以输入用户并通过。这是代码的一部分:
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge{
NSLog(@"Need Authentication");
UIAlertView *webLogin = [[UIAlertView alloc] initWithTitle:@"Authentication"
message:@"Enter User and Pass"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK"
, nil];
webLogin.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[webLogin show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
user = [[alertView textFieldAtIndex:0]text];
pass = [[alertView textFieldAtIndex:1]text];
NSLog(@"user is %@ and pass is %@",user,pass);
if (buttonIndex == [alertView cancelButtonIndex]) {
[self dismissModalViewControllerAnimated:YES];
}
else if (buttonIndex != [alertView cancelButtonIndex]) {
NSLog(@"OK Pressed");
[self handleAuthentificationOKForChallenge:nil withUser:user password:pass];
}
}
- (void)handleAuthentificationOKForChallenge:(NSURLAuthenticationChallenge *)aChallenge withUser:(NSString *)userName password:(NSString *)password {
NSURLCredential *credential = [[NSURLCredential alloc]
initWithUser:userName password:password
persistence:NSURLCredentialPersistenceForSession];
[[aChallenge sender] useCredential:credential forAuthenticationChallenge:aChallenge];
}
谁能告诉我如何调用 handleAuthenticationOKForChallenge 我对 NSURLAuthenticationChallenge 有点困惑......