Technical Note TN2232: HTTPS Server Trust Evaluation lays out the recommended best practices for using a self signed certificate in an iOS application. The recommended approach is to implement the TLS trust evaluation like SSL pinning: trusting a specific certificate or public key. Implementing this was demonstrated in the WWDC 2014 session Building Apps for Enterprise and Education. Unfortunately the code for SSL pinning has not been released as sample code, nor is it available in the slides - only the video. The process of evaluating the server trust is explained well in the session.
This does not solve your problem of persisting the evaluated server trust. The server trust provided by the authentication challenge represents the state of the SSL transaction, and as such cannot really be persisted in a meaningful way. This is why NSURLCredential
's constructor for using a SecTrustRef does not have a parameter for NSURLCredentialPersistence
: server trust must be per session or transaction.
That said, the URL loading system does allow for default handling of SSL/TLS server trust evaluation. Normally for HTTPS connections you do not need to implement an authentication challenge handler for NSURLAuthenticationMethodServerTrust
. If the default trust evaluation failed your connection would fail with some familar errors - this is what happens when using a self signed certificate, because the certificate is not trusted. It may be possible to add your trusted, self signed certificate to the application's trust anchors (much as you do in trust evaluation for SSL pinning) and the default handling would "just work" from that point on. Unfortunately I do not currently have a test environment in which I can test this.