我想通过AFNetworking允许无效的SSL证书

Fee*_*ics 27 objective-c afnetworking

我想允许无效的SSL证书.我的主要代码如下:

myClient = [[MyClient alloc] init];
[myClient getHtml:@"/path/to/the/distination.html"];
Run Code Online (Sandbox Code Playgroud)

MyClient类代码如下:

#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
Run Code Online (Sandbox Code Playgroud)

我阅读下面的页面并尝试了宏但这不起作用.

自签名证书SSL·问题#189·AFNetworking/AFNetworking https://github.com/AFNetworking/AFNetworking/issues/189

请帮我...

tit*_*coy 45

在AFNetworking 2.0中,您可以使用以下内容:

[AFHTTPRequestOperationManager manager].securityPolicy.allowInvalidCertificates = YES;
Run Code Online (Sandbox Code Playgroud)


Avt*_*iya 40

允许使用AFNetworking的无效SSL证书.在#import Availability.h下面的AFURLConnectionOperation.h中添加以下行

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1
Run Code Online (Sandbox Code Playgroud)

  • 从版本1.2.1开始,您不再需要设置此定义.相反,您可以在AFHTTPRequestOperation上将'allowsInvalidSSLCertificate'属性设置为YES,这样更方便. (13认同)
  • 有没有办法让allowsInvalidSSLCertificate与UIImageView + AFNetworking一起使用? (6认同)

Dav*_*vid 24

您现在可以使用AFHTTPClient的allowsInvalidSSLCertificate属性.无需在最新版本的AFNetworking中使用定义.

AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:@"url"];
client.allowsInvalidSSLCertificate = YES; //this defaults to no
Run Code Online (Sandbox Code Playgroud)


Seb*_*dal 12

我正在使用RestKit

client.allowsInvalidSSLCertificate = YES;
Run Code Online (Sandbox Code Playgroud)

不起作用.该选项不会传播到restkit创建的操作.

我正在使用cocoapods,因此pch文件或pods项目中的任何更改都会被覆盖.我一直在使用的"hack"是一个cocoapod安装后操作,它添加了所需的预处理器定义.在我的pod文件的末尾,我添加了:

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
Run Code Online (Sandbox Code Playgroud)


mat*_*ttt 8

请注意,如果您通过CocoaPods进行安装,则#define在项目中使用此宏是不够的 - 编译静态库时必须设置编译器宏才能使其生效.

  • "编译静态库时必须设置编译器宏才能使其生效" - 你是怎么做到的? (2认同)

小智 5

以下是使用管理器进行POST操作的方法.

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //initialize
 manager.securityPolicy.allowInvalidCertificates=YES;    //allow unsigned
 manager.responseSerializer=[AFJSONResponseSerializer serializer];   //set up for JSOn  
 [manager POST:@"myweb.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
     //success stuff
 }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     //error stuff
}];
Run Code Online (Sandbox Code Playgroud)

编辑 - 快速版:

    var securityPolicy = AFSecurityPolicy()
    securityPolicy.allowInvalidCertificates = true;
    manager.securityPolicy = securityPolicy;
    manager.POST(
        "https://exmaple.com/foobar.php",
        nil,
    ...
Run Code Online (Sandbox Code Playgroud)