これは明らかに、自己署名されていない証明書がある場合、または追加した場合にのみ機能します。
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ を pch ファイルに追加します。これにココア ポッドを使用している場合は、AFHTTPRequestOperation をサブクラス化し、以下を実装する必要があります。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([self bypassSslCertValidation:protectionSpace])
return YES;
else
return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];
}
return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if ([self bypassSslCertValidation:challenge.protectionSpace]) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
return;
}
else
return [super connection:connection didReceiveAuthenticationChallenge:challenge];
return;
}
}
- (BOOL) bypassSslCertValidation:(NSURLProtectionSpace *) protectionSpace
{
if (ENVIRONMENT_TYPE == DEV_ENV || ENVIRONMENT_TYPE == STAGING_ENV) {
return YES;
}
return NO;
}
次に、AFNEtworking に新しいサブクラスを使用するように指示します。
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
[client registerHTTPOperationClass:[YourSubClassHTTPRequestOperation class]];
これは世界で最も簡単なことではなく、技術的に自己署名を無視しても機能しませんが、標準の SLL 証明書を使用すれば問題なく機能する可能性があります。このコードを削除するか、デバッグ時にのみ使用できるようにすることを忘れないでくださいリリースする予定がある場合。
コメントには文字制限があるため、回答に追加してください!
ヘッダーを見ていくつかの選択肢
キューに手動で追加できる戻り操作:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
または、カスタム サブクラス操作をこれに渡します。
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;