iosswiftalamofirensapptransportsecurity

How to trust an endpoint iOS swift


I'm calling an endpoint that has a self-signed ssl certificate i have tried adding this in my info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

But i am still not able to access the endpoint i keep getting this

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

Solution

  • You need to create a session manager and tell it to disable evaluation of the ssl in that server.

    Something like this

    static var manager: Alamofire.SessionManager = {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "https://example.com": .disableEvaluation
        ]
    
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
        let manager = Alamofire.SessionManager(
            configuration: URLSessionConfiguration.default,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    
        return manager
    }()
    

    An then, instead of calling request in Alamofire, like this

    Alamofire.request("https://example.com", method: .get…
    

    you call it in your manager

    manager.request("https://example.com"…