Apple has Technical Note 2232 , which is quite informative and describes in detail the assessment of the trust of an HTTPS server .
In this case, error -1202 in the NSURLErrorDomain domain is NSURLErrorServerCertificateUntrusted , which means that the server trust check failed. You can also get many other errors; Appendix A: General Server Trust Validation Errors are listed the most common.
From the Technical Note:
In most cases, the best way to allow server trust assessment to fail is to fix the server. This has two advantages: it offers better security, and it reduces the amount of code you have to write. the rest of this technote describes how you can diagnose server trust with evaluation errors and, if it is not possible to install a server, how you can configure server trust verification to continue without connecting your user completely.
The specific bit that is related to this question is the NSURLSession server trust assessment section:
NSURLSession allows you to configure HTTPS server trust verification to implement the -URLSession:didReceiveChallenge:completionHandler: delegate. To configure an HTTPS server trust assessment, find a task whose security space has the NSURLAuthenticationMethodServerTrust authentication NSURLAuthenticationMethodServerTrust . To solve these problems as described below. For other problems that you don’t care, call the completion handler block using the NSURLSessionAuthChallengePerformDefaultHandling and NULL credentials.
When working with the NSURLAuthenticationMethodServerTrust authentication call, you can get the trust object from the security call by calling the -serverTrust method. After using the trust object to execute the HTTPS server’s own user trust, you must solve the problem in one of two ways:
If you want to reject the connection, call the termination handler block using the NSURLSessionAuthChallengeCancelAuthenticationChallenge location and NULL credentials.
If you want to allow the connection, create credentials from your trust object (using +[NSURLCredential credentialForTrust:] ) and call the completion handler block with the specified credentials and NSURLSessionAuthChallengeUseCredential .
The bottom line of all this is that if you implement the following delegate method, you can override the server trust for a specific server:
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler { if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { if([challenge.protectionSpace.host isEqualToString:@"domaintoverride.com"]) { NSURLCredential *credential = [NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]; completionHandler(NSURLSessionAuthChallengeUseCredential,credential); } else completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); } }
Note that you must handle both the host case corresponding to the one you want to override and all other cases. If you do not handle the "all other cases" part, the result of the behavior is undefined.