title: How do I accept a self-signed SSL certificate using iOS 7's NSURLSession and its family of delegate methods for development purposes?
body: <p>I am developing an iPhone app. <em>During development</em>, I need to connect to a server that's using a self-signed SSL certificate. I'm pretty certain <code>- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler</code> is my opportunity to write some exception code to allow this. However, I can't find any resources that tell me how to do this. I can see the following error in the log:</p>
  <pre><code>NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
  </code></pre>
  <p>In addition to this, when I <code>NSLog(@&quot;error = %@&quot;, error);</code> from within the above delegate method I get:</p>
  <blockquote>
  <p>Error Domain=NSURLErrorDomain Code=-1202 &quot;The certificate for
  this server is invalid. You might be connecting to a server that is
  pretending to be <code>api.mydevelopmenturl.example</code> which could put your
  confidential information at risk.&quot; UserInfo=0x10cbdbcf0
  {NSUnderlyingError=0x112ec9730 &quot;The certificate for this server is
  invalid. You might be connecting to a server that is pretending to be
  <code>api.mydevelopmenturl.example</code> which could put your confidential information
  at risk.&quot;, NSErrorFailingURLStringKey=https://api.mydevelopmenturl.example/posts,
  NSErrorFailingURLKey=https://api.mydevelopmenturl.example/posts,
  NSLocalizedRecoverySuggestion=Would you like to connect to the
  server anyway?, NSURLErrorFailingURLPeerTrustErrorKey=&lt;SecTrustRef: 0x112e5a020&gt;,
  NSLocalizedDescription=The certificate for this server is invalid.
  You might be connecting to a server that is pretending to be
  <code>api.mydevelopmenturl.example</code> which could put your confidential
  information at risk.}</p>
  </blockquote>
  <p>Any ideas on how to resolve this issue? Please post code as I've read the conceptual docs and I don't understand them. Here's an example of one that's beyond me: <a href="https://developer.apple.com/library/content/technotes/tn2232/_index.html" rel="nofollow noreferrer">https://developer.apple.com/library/content/technotes/tn2232/_index.html</a></p>