这显然只有在您拥有非自签名证书或添加以下内容时才有效:
#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;