AFNetworking 3 issue
Asked Answered
A

3

2

In AFNetworking 3 for invalid SSL certificate I used validatesCertificateChain = false , but now it seems that this field was removed and I can't make requests to my server.

Here is class for requests:

import UIKit
import AFNetworking

class ClientHTML: AFHTTPSessionManager {
    private static var __once: () = { () -> Void in

        let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate)
        securityPolicy.validatesDomainName = false
        securityPolicy.allowInvalidCertificates = true
        sharedInstanceTemp.securityPolicy = securityPolicy


        sharedInstanceTemp.requestSerializer = AFHTTPRequestSerializer()
        sharedInstanceTemp.responseSerializer = AFHTTPResponseSerializer()


    }()

    fileprivate static let sharedInstanceTemp = ClientHTML(baseURL: URL(string: kServer_urlBilderlings))

    static var sharedInstance:ClientHTML {
        get {
            _ = ClientHTML.__once

            return sharedInstanceTemp
        }
    }
}

I do requests by:

ClientHTML.sharedInstance.post("https://acs-web-test.firstdata.lv", parameters: nil, progress: { (progress) in
            print("progress = ", progress)
        }, success: { (task, response) in
            let data = response as! Data
            let html = String(data: data, encoding: .utf8)
            print("success responce = ", html)
        }, failure: { (task, error) in
            print("error = ", error)
        })

And according to this post I have to use validatesCertificateChain property. Can anybody help?

Adah answered 16/11, 2016 at 5:19 Comment(2)
I still see that, allowInvalidCertificates property exists in AFSecurityPolicy in AFNetworking 3.0.0, please refer this document - cocoadocs.org/docsets/AFNetworking/3.0.0-beta.1/Classes/…Floeter
I use this property, but I need another validatesCertificateChain, I need to set it to false. Please, take a look at this question's best answer #27808749 . They said about this property, but it was afnetworking 2Adah
A
2

I solved it by changing let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.certificate) to let securityPolicy = AFSecurityPolicy(pinningMode: AFSSLPinningMode.none)

Adah answered 16/11, 2016 at 5:51 Comment(1)
Thanks for posting your answer which would help someone later on.Floeter
J
2

For objective-c user setAllowInvalidCertificates:YES without SSL Pinning:

AFSecurityPolicy *sec = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[sec setAllowInvalidCertificates:YES];
[sec setValidatesDomainName:NO];
manager.securityPolicy = sec;
J answered 19/4, 2018 at 9:37 Comment(0)
S
0

Objective-C

static NSString* const kSecurityCertificate = @"xxxxx";

yourManager.securityPolicy = [self customSecurityPolicy];

- (AFSecurityPolicy*)customSecurityPolicy {
   AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
   NSString *cerPath = [[NSBundle mainBundle] pathForResource:kSecurityCertificate ofType:@"der"];
   NSData *certData = [NSData dataWithContentsOfFile:cerPath];
   [securityPolicy setValidatesDomainName:NO];
   [securityPolicy setAllowInvalidCertificates:YES];
   [securityPolicy setPinnedCertificates:[NSSet setWithObjects:certData, nil]];
   return securityPolicy;
}
Shushan answered 24/9, 2020 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.