Certificate pinning in Alamofire
<p>I am creating an iPad app that accesses HTTPS web services. I want to implement pinning, but am having issues.</p>

<p>This class creates the Alamofire Manager (mostly taken from documentation):</p>

<pre><code>class NetworkManager {

    var manager: Manager?

    init() {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "www.google.co.uk": .PinCertificates(
                certificates: ServerTrustPolicy.certificatesInBundle(),
                validateCertificateChain: true,
                validateHost: true
            ),
            "insecure.expired-apis.com": .DisableEvaluation
        ]

        manager = Alamofire.Manager(
            configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    }
}
</code></pre>

<p>This function makes the call:</p>

<pre><code>static let networkManager = NetworkManager()

public static func testPinning() {
    networkManager.manager!.request(.GET, "https://www.google.co.uk").response { response in
        if response.1 != nil {
            print("Success")
            print(response.1)
            print(response.1?.statusCode)
        } else {
            print("Error")
            print(response.3)
        }
    }
}
</code></pre>

<p>The <a href="https://superuser.com/a/97203/156776">certificate is saved</a> in the project and shows under 'Targets > Build Phases > Copy Bundle Resources'.</p>

<p>I am currently receiving the following error every time I make the request (from the else block in <code>testPinning()</code>):</p>

<pre><code>Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://www.google.co.uk/, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://www.google.co.uk/})
</code></pre>