Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

How do I turn off validation for certificate with Alamofire?

I simply create Session like this:

    let configuration = URLSessionConfiguration.default
    configuration.urlCache = nil
    let session = Alamofire.Session(configuration: configuration)

When I create requests, I get following error:

The certificate for this server is invalid. You might be connecting to a server that is pretending to be “my.serwer.pl” which could put your confidential information at risk.

How can I disable it to not to check if certificate is valid?

For objective-c and AFNetworking it was like this:

    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];

    [securityPolicy setValidatesDomainName:NO];

    [securityPolicy setAllowInvalidCertificates:YES];

    manager.securityPolicy = securityPolicy;

Upvotes: 3

Views: 3526

Answers (2)

Titouan
Titouan

Reputation: 581

Disabling verification for an IP adress - Alamofire

There is unfortunately no way of disabling verification for all servers but if you want to disable the verification for certain server, you will need to create the Alamofire session with a server trust manager :

// Disabling verification for 0.0.0.0 (replace with your own server of course).
let manager = ServerTrustManager(evaluators: ["0.0.0.0": DisabledTrustEvaluator()])
// Creating the session.
let session = Session(serverTrustManager: manager)

You can then use your session as normal and the requests should go through. If you are working with multiple different server you can add them to the list like this :

let manager = ServerTrustManager(evaluators: [
   "0.0.0.0": DisabledTrustEvaluator(), 
   "1.1.1.1" : DisabledTrustEvaluator()
])

I just had this issue a few days ago so I did lots of research on it and if this still doesnt work for you, I may be able to give you another solution in order to make it work so just ask in comments.

Upvotes: 2

NaveedUlHassan5
NaveedUlHassan5

Reputation: 265

I had exactly the same error when i tried to Call API using Alamofire, so to bypass this error I created a session with the help of some answer from SO.

private let session: Session = {
        let manager = ServerTrustManager(evaluators: ["yourDomainName": DisabledTrustEvaluator()])
        let configuration = URLSessionConfiguration.af.default
        return Session(configuration: configuration, serverTrustManager: manager)
    }()

and instead of AF.request, I used session.request. That's all.

Upvotes: 4

Related Questions