httpsの証明書切れのページにNSURLConnectionでつなぐ
繋がらなくてちょびっとだけハマった。
つなごうとすると、以下の様なエラーを吐きます。
Error Domain=NSURLErrorDomain Code=-1202
内容を読んでみると、「証明書切れで危ないから接続させませんよ!」的な内容が帰ってくる。
こういう時は、以下をNSURLConnectionのDelegateメソッドとして追加すると良い。
// 証明書切れ回避 - (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]){ [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; } [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; }
これで問題なくつながる。
よかったよかった。
コメントする