NSURLConnection の delegate 実装のこの willSendRequestForAuthenticationChallenge メソッドは、
BASIC認証でも、SSL認証でも実行される。
従って、両方処理したいなら以下のように、、、
-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]
|| [challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]) {
// BASIC/DIGEST認証が必要
[self performSelectorOnMainThread:@selector(challengeBasic) withObject:nil waitUntilDone:YES];
return;
}else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
// SSL認証時
NSURLResponse *response;
NSError *error = nil;
[ NSURLConnection sendSynchronousRequest:self.request
returningResponse:&response
error:&error ];
if (error==nil) {
// SSL認証を通す
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}else{
// [error code]==NSURLErrorServerCertificateUntrusted など任意の処理を、、
NSInteger preFailCount = [challenge previousFailureCount];
}
}
}
Android のように、BASIC認証用のハンドラを用意して欲しいが、そもそも
API階層が全くことなるから仕方ない。