,如果你有一個非自簽名的證書或將您加入這顯然只工作:
的#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證書它很可能會工作得很好,請記住刪除此代碼或僅在調試時纔可用,如果您計劃發佈。
添加到答案,因爲評論有字符限制!
幾個選擇看,可以手動添加到隊列中的headers
返回操作:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
或者在您的自定義子類的操作傳遞到這一個:
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
報道在日誌什麼錯誤?另外,你的服務器證書是否有效? – nioq 2013-02-15 18:23:50