验证自签名证书机智AFNetworking

时间:2013-04-30 09:59:41

标签: ios certificate afnetworking self-signed general-network-error

我想通过自签名证书将我的iOS应用程序与AFNetwork连接到我的网络服务器。我在github上找到了一个解决方案(https://github.com/AFNetworking/AFNetworking/pull/694)我试了一下,证书固定似乎有效,但我还有一个错误:

  

错误域= NSURLErrorDomain代码= -1012“无法完成操作。   (NSURLErrorDomain错误-1012。)“UserInfo = 0x7bc2090 {NSErrorFailingURLKey =(my domain)}

有人知道这个错误是否与AFNetworking Framework和自签名证书有关吗?

解决: 我找到了错误的解决方案。我现在必须将SSLPinningMode设置为AFSSLPinningModeCertificate。

AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"%@",error);
}];
operation.SSLPinningMode = AFSSLPinningModeCertificate;
[operation start];

1 个答案:

答案 0 :(得分:0)

尝试这项工作。

AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSDictionary *resDictionary = (NSDictionary *)JSON;
}
                                                                                   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                       NSLog(@"%@",error);
                                                                                   }];
operation.securityPolicy.allowInvalidCertificates = YES;
[operation start];