I want to allow invalid SSL certificates with AFNetworking

35,243

Solution 1

To allow Invalid SSL Certificate with AFNetworking. Add the following line in AFURLConnectionOperation.h below #import Availability.h

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1

Solution 2

In AFNetworking 2.0, you can use the following:

[AFHTTPRequestOperationManager manager].securityPolicy.allowInvalidCertificates = YES;

Solution 3

You can now use the allowsInvalidSSLCertificate property of the AFHTTPClient. No need to use defines in the latest versions of AFNetworking.

AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:@"url"];
client.allowsInvalidSSLCertificate = YES; //this defaults to no

Solution 4

I am using RestKit so

client.allowsInvalidSSLCertificate = YES;

does not work. The option is not propagated to the operations created by restkit.

I am using cocoapods so any changes in the pch file or the pods project get overriden. The "hack" I have been using is a cocoapod post-install operation that adds the required preprocessor definition. At the end of my pod file I have added:

post_install do |installer_representation|
  installer_representation.project.targets.each do |target|
    if target.name == 'Pods-AFNetworking'
      target.build_configurations.each do |config|
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << '_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1'
      end
    end
  end
end

Solution 5

Note that if you are installing through CocoaPods, #define-ing this macro in your project will not be enough--the compiler macro must be set when compiling the static library in order for it to take effect.

Share:
35,243

Related videos on Youtube

Feel Physics
Author by

Feel Physics

I developed Physics education app using mixed reality, and gave experience sessions / lessons in five countries including Africa.

Updated on June 15, 2020

Comments

  • Feel Physics
    Feel Physics about 4 years

    I want to allow invalid SSL certificates. My main code is below:

    myClient = [[MyClient alloc] init];
    [myClient getHtml:@"/path/to/the/distination.html"];
    

    The MyClient class code is below:

    #import <Foundation/Foundation.h>
    #import "AFNetworking.h"
    
    @interface MyClient : NSObject
    
    - (void)getHtml:(NSString *)path;
    
    @end
    
    #define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
    
    @implementation MyClient
    
    - (void)getHtml:(NSString *)path
    {
        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://trusted.server.net"]];
        [httpClient getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"%@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error = %@", error);
        }];
    }
    
    @end
    

    I read below page and tried the macro but this doesn't work.

    Self Signed certificate SSL · Issue #189 · AFNetworking/AFNetworking https://github.com/AFNetworking/AFNetworking/issues/189

    Please help me...

  • Carl Veazey
    Carl Veazey over 11 years
    Why do you need to subclass for this? Seems you could just add the protection space and authentication challenge blocks from anywhere, or am I missing something?
  • bobics
    bobics about 11 years
    If you want to allow self-signed certs programmatically and still use AFHTTPClient, you need to do it this way. Unfortunately there's no equivalent setAuthenticationAgainstProtectionSpaceBlock for AFHTTPClient.
  • Phil
    Phil about 11 years
    As of version 1.2.1, you don't need anymore to set this define. Instead you can set the 'allowsInvalidSSLCertificate' property to YES on AFHTTPRequestOperation, which is much more convenient.
  • Richard Venable
    Richard Venable about 11 years
    "the compiler macro must be set when compiling the static library in order for it to take effect" - How do you do that?
  • Kyle Clegg
    Kyle Clegg about 11 years
    If you do this, make sure you are only doing it in debug mode. If you are hitting HTTPS endpoints and you allow invalid certs you're losing a good portion of the security you're getting from SSL.
  • Ian Kershaw
    Ian Kershaw almost 11 years
    Is there any way to get allowsInvalidSSLCertificate to work with UIImageView+AFNetworking?
  • Humber
    Humber over 10 years
    On iOS7 and XCode 5 this did the trick for me. I'm using AFNetworking 1.3.3
  • Carlos Ricardo
    Carlos Ricardo over 10 years
    OMG! Thank you so much!
  • bleeckerj
    bleeckerj over 10 years
    This helped me as well. Good stuff.
  • Leszek Zarna
    Leszek Zarna over 10 years
    currently objectManager.HTTPClient.allowsInvalidSSLCertificate = YES; or something similar in RestKit works
  • Leszek Zarna
    Leszek Zarna over 10 years
    currently objectManager.HTTPClient.allowsInvalidSSLCertificate = YES; or something similar in RestKit works
  • ingh.am
    ingh.am almost 9 years
    In the latest version of AFNetworking (I'm running 2.2.3) you now need to use AFSecurityPolicy and set it as the 'security' property on AFHTTPRequestOperation. See stackoverflow.com/a/20815306/143979 for more info.
  • Suvo08 K
    Suvo08 K over 8 years
    That helps. I added "self.securityPolicy.allowInvalidCertificates = YES;" in AFHTTPRequestOperationManager init method.
  • Nikunj
    Nikunj over 6 years
    hello, i have add #define AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES 1 in my project AFURLConnectionOperation.h below #import Availability.h file but i getting This Error in consol Error: Error Domain=NSURLErrorDomain Code=-1012 "(null)" UserInfo={NSErrorFailingURLKey=my Url.com, NSErrorFailingURLStringKey=my Url.com} @AvtarSingh Suchariya