This, obviously, will only work if you have a self-signed certificate OR you add:
#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ to your pch file. If you use cocoa pods for this, you will probably need to subclass AFHTTPRequestOperation and implement:
- (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; }
Then tell AFNEtworking to use the new subclass:
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]]; [client registerHTTPOperationClass:[YourSubClassHTTPRequestOperation class]];
This is not the easiest thing in the world, and technically ignoring the self-signed does not make it work, but if you use standard SLL certificates, it will probably work fine, do not forget to remove this code or make it only available for debugging if you plan to release .
Adding an answer because comments have char restrictions!
Several heading options
Return operation, which can be manually added to the queue:
- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
Or go to your own subclass operation for this:
- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
Holyprin
source share