title: "An SSL error has occurred and a secure connection to the server cannot be made."
body: <p>In iOS 10.2 we are seing this error when we are trying to communicate with several different apis. The wierd thing is that we are only seing this sometimes, in ~ 2 out of 200 requests to the same url. But on the other hand, I can sometimes do 3000 api calls, without seeing the issue.</p>
  
  <p>As far as I've read this is related to ATS. The output also shows us that we are getting an error code that relates to ATS: kCFStreamErrorCodeKey=-9806.</p>
  
  <p>However I do not understand how this can relate to ATS, because of a couple of points:</p>
  
  <ul>
  <li>The url I am calling is https.</li>
  <li>The server has a valid TLSv1.2 certificate </li>
  <li>I have tried to create an exception, but it doesn't seem to do anything</li>
  </ul>
  
  <p>We've tried to create the exception in different ways, both by allowing arbitary loads, and created the exception for the domain.</p>
  
  <p>Even though it shouldn't be necessary when the server has a valid certificate we have tried to create the exception</p>
  
  <p>This was how we created the exception for the domain.</p>
  
  <pre><code>&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
      &lt;dict&gt;
        &lt;key&gt;NSExceptionDomains&lt;/key&gt;
        &lt;dict&gt;
          &lt;key&gt;deezer.com&lt;/key&gt;
          &lt;dict&gt;
            &lt;key&gt;NSIncludesSubdomains&lt;/key&gt;
            &lt;true/&gt;
            &lt;key&gt;NSTemporaryExceptionAllowsInsecureHTTPLoads&lt;/key&gt;
            &lt;true/&gt;
            &lt;key&gt;NSTemporaryExceptionMinimumTLSVersion&lt;/key&gt;
            &lt;string&gt;TLSv1.2&lt;/string&gt;
            &lt;key&gt;NSTemporaryExceptionRequiresForwardSecrecy&lt;/key&gt;
            &lt;false/&gt;
          &lt;/dict&gt;
        &lt;/dict&gt;
      &lt;/dict&gt;
  </code></pre>
  
  <p>And this was how we enabled arbitary loads:</p>
  
  <pre><code>&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
    &lt;dict&gt;
      &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
      &lt;true/&gt;
    &lt;/dict&gt;
  </code></pre>
  
  <p>Another wierdness is that we are only seing this in iOS 10 (10.2). When we are testing in iOS 9.3 everything works as expected.</p>
  
  <p>I've made a sample app to reproduce this in. </p>
  
  <pre><code>- (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      [self Perform];
  }
  
  int i = 0;
  
  - (void) Perform
  {
      NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  
      NSURLSession *sessionWithoutADelegate = [NSURLSession sessionWithConfiguration:defaultConfiguration];
      NSURL *url = [NSURL URLWithString:(@"https://api.deezer.com/chart/0/tracks")];
  
      [[sessionWithoutADelegate dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
          if(error != NULL)
          {
              NSLog(@"Got response with error %@.\n", error);
          }
  
          NSLog(@"%d",i);
          i++;
          if(i &lt;1000)
          {
              [self Perform];
          }
  
  
      }] resume];
  }
  </code></pre>
  
  <p>Does anybody have an idea for solutions? Thanks</p>