SSLウェブページに接続するための次の簡単なコードがあります
NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
[ NSURLConnection sendSynchronousRequest: urlRequest returningResponse: nil error: &error ];
ただし、証明書が自己署名されたものである場合はエラーが発生します。Error Domain=NSURLErrorDomain Code=-1202 UserInfo=0xd29930 "untrusted server certificate".
とにかく接続を受け入れるように設定する方法(ブラウザで「承認」を押すのと同じように)またはそれをバイパスする方法はありますか?
ベストアンサー1
これを実現するためのサポートされている API があります。NSURLConnection
デリゲートに次のようなものを追加します。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
if ([trustedHosts containsObject:challenge.protectionSpace.host])
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
connection:didReceiveAuthenticationChallenge:
必要に応じてユーザーにダイアログ ボックスを表示した後など、かなり後になってから challenge.sender にメッセージを送信できることに注意してください。