How to allow https connection using AFNetworking?

I have AFNetworking configured, but it does not accept https requests. How can I get AFNEtworking to connect via ssl.

I have the following code:

NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path: pathstr parameters: params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { //TODO: attach file if needed }]; AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { //success! completionBlock(responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //failure :( NSLog(@"%@", error); completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]); }]; [operation start]; 
+7
source share
4 answers
 operation.securityPolicy.allowInvalidCertificates = YES; 

This code is very important. If you do not add this, you will receive an error message.

+14
source

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; 
+6
source

Try this code.

  NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path: pathstr parameters: params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { //TODO: attach file if needed }]; AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { //success! completionBlock(responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //failure :( NSLog(@"%@", error); completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]); }]; operation.securityPolicy.allowInvalidCertificates = YES; [operation start]; 
+2
source

If AFNetworking disables the subclass over the implementation of the superclass, then just enter the code, for example: http // foo // bar, and set the value to bool for this

0
source

All Articles