I want to allow invalid SSL certificates with AFNe

2019-01-10 08:22发布

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...

13条回答
女痞
2楼-- · 2019-01-10 08:43

if you are using RestKit using

client.allowsInvalidSSLCertificate = YES;

won't work, instead do this:

in your project, click on RestKit.xcodeproj go to project > Build Settings > Preprocessor Macros

and add _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1

thats finished.

查看更多
ら.Afraid
3楼-- · 2019-01-10 08:43

if you are using RestKit using

client.allowsInvalidSSLCertificate = YES;

won't work, instead do this:

if you added rest kit manually to your project, click on RestKit.xcodeproj go to project > Build Settings > Preprocessor Macros

and add _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1

thats finished.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-10 08:45

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
查看更多
在下西门庆
5楼-- · 2019-01-10 08:45

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
查看更多
别忘想泡老子
6楼-- · 2019-01-10 08:45

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.

查看更多
叛逆
7楼-- · 2019-01-10 08:52

I encountered the same problem and no one of the solutions that I read works.

For me the only workaround is set the AFSecurityPolicy like this:

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy = securityPolicy;

I decided to reply although the question is old, maybe can help someone.

查看更多
登录 后发表回答