Ciò, ovviamente, funziona solo se si dispone di un cert non auto-firmato o si aggiunge:
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ al file PCH. Se si utilizza baccelli di cacao per questo è probabile che sia necessario sottoclasse AFHTTPRequestOperation e implementare:
- (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;
}
Poi dire AFNEtworking per utilizzare la nuova sottoclasse:
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
[client registerHTTPOperationClass:[YourSubClassHTTPRequestOperation class]];
Non è la cosa più facile da fare in il mondo e ignorare tecnicamente l'autofirmato non lo sta facendo funzionare, ma se si utilizzano certificati SLL standard è probabile che funzionerà correttamente, ricordarsi di rimuovere questo codice o renderlo disponibile solo durante il debug se si prevede di rilasciare.
Aggiunta alla risposta perché i commenti hanno limiti di caratteri!
Poche scelte guardando l'operazione headers
di ritorno che può essere aggiunto manualmente alla coda:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
O passano nelle vostre operazioni sottoclasse personalizzata a questo:
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
Quale errore è riportato nel registro? Inoltre, il certificato del server è valido? – nioq