title: An SSL error has occurred and a secure connection to the server cannot be made
body: <p>As title says. I have tried using both NSURLSession and Alamofire. </p>
  
  <p>Both are giving me the same errors. I am using the simulator and don't have access to a device. I have limitations and need it to work on the simulator.</p>
  
  <p>I am trying to retrieve userlogin details from the server (again the server i don't have access to as another team is responsible.</p>
  
  <pre><code>let headers = [
      "content-type": "application/json",
      "accept": "application/json",
      "appid": "test",
      "cache-control": "no-cache",
      "postman-token": "xxxx-xxxx-xxxx-xxxx-xxxx"
  ]
  let parameters = ["attributes": [
      "password": "test",
      "devicename": "My smartphone",
      "deviceid": "ABC99999999",
      "email": "xxxx@xxxx.co.uk"
      ]]
  
  
  do {
  
      let postData = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)
  
      let request = NSMutableURLRequest(URL: NSURL(string: "https://a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net:443/Identity/rest/appl/test/wflow/auth")!,
                                        cachePolicy: .UseProtocolCachePolicy,
                                        timeoutInterval: 10.0)
      request.HTTPMethod = "POST"
      request.allHTTPHeaderFields = headers
      request.HTTPBody = postData
  
      let session = NSURLSession.sharedSession()
      let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -&gt; Void in
          if (error != nil) {
              print(error?.localizedDescription)
          } else {
              let httpResponse = response as? NSHTTPURLResponse
              print(httpResponse)
          }
      })
  
      dataTask.resume()
  
  } catch {
  
      print(error)
  }
  </code></pre>
  
  <p>The Url is only for internal use so will not work outside our network.</p>
  
  <p>These are the errors that i am getting constantly.</p>
  
  <p><strong>"An SSL error has occurred and a secure connection to the server cannot be made."</strong></p>
  
  <p><strong>"The certificate for this server is invalid. You might be connecting to a server that is pretending to be “a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net” which could put your confidential information at risk."</strong></p>
  
  <p>I have searched and searched online but can not find any solution.</p>
  
  <p>This is what my info.plist looks like:</p>
  
  <pre><code>&lt;key&gt;NSAppTransportSecurity&lt;/key&gt;
  &lt;dict&gt;
      &lt;key&gt;NSAllowsArbitraryLoads&lt;/key&gt;
      &lt;true/&gt;
      &lt;key&gt;NSExceptionDomains&lt;/key&gt;
      &lt;dict&gt;
          &lt;key&gt;a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net&lt;/key&gt;
          &lt;dict&gt;
              &lt;key&gt;NSRequiresCertificateTransparency&lt;/key&gt;
              &lt;string&gt;NO&lt;/string&gt;
              &lt;key&gt;NSThirdPartyExceptionMinimumTLSVersion&lt;/key&gt;
              &lt;string&gt;TLSv1.2&lt;/string&gt;
              &lt;key&gt;NSThirdPartyExceptionRequiresForwardSecrecy&lt;/key&gt;
              &lt;string&gt;YES&lt;/string&gt;
              &lt;key&gt;NSThirdPartyExceptionAllowInsecureHTTPSLoads&lt;/key&gt;
              &lt;false/&gt;
              &lt;key&gt;NSExceptionMinimumTLSVersion&lt;/key&gt;
              &lt;string&gt;TLSv1.2&lt;/string&gt;
              &lt;key&gt;NSExceptionRequiresForwardSecrecy&lt;/key&gt;
              &lt;true/&gt;
              &lt;key&gt;NSExceptionAllowsInsecureHTTPLoads&lt;/key&gt;
              &lt;true/&gt;
              &lt;key&gt;NSIncludeSubdomains&lt;/key&gt;
              &lt;true/&gt;
          &lt;/dict&gt;
      &lt;/dict&gt;
  &lt;/dict&gt;
  </code></pre>
  
  <p>I have looked at the following posts and applied them already without any success:</p>
  
  <p><a href="https://stackoverflow.com/questions/31945078/how-to-connect-to-self-signed-servers-using-alamofire-1-3?rq=1">How to connect to self signed servers using Alamofire 1.3</a></p>
  
  <p><a href="https://stackoverflow.com/questions/36331623/ios-9-3-an-ssl-error-has-occurred-and-a-secure-connection-to-the-server-cannot?rq=1">iOS 9.3 : An SSL error has occurred and a secure connection to the server cannot be made</a></p>
  
  <p><a href="https://stackoverflow.com/questions/32755674/ios9-getting-error-an-ssl-error-has-occurred-and-a-secure-connection-to-the-ser">iOS9 getting error “an ssl error has occurred and a secure connection to the server cannot be made”</a></p>
  
  <p><strong>UPDATE 1:</strong>
  I forgot to add that when i used Alamofire, i also tried to ignore the server policies or bypass them via the methods below, again none of them worked :(</p>
  
  <pre><code>let manager : Alamofire.Manager = {
      // Create the server trust policies
      let serverTrustPolicies: [String: ServerTrustPolicy] = [
          "a-eco-oid-web-ppf-slo-vs-d.stbc2.jstest2.net": .DisableEvaluation
      ]
      // Create custom manager
      let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
      configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
      let man = Alamofire.Manager(
          configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
          serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
      )
      return man
  }()
  
  let manager = Alamofire.Manager.sharedInstance
  manager.delegate.sessionDidReceiveChallenge = { session, challenge in
      var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling
      var credential: NSURLCredential?
      if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
          disposition = NSURLSessionAuthChallengeDisposition.UseCredential
          credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
      } else {
          if challenge.previousFailureCount &gt; 0 {
              disposition = .CancelAuthenticationChallenge
          } else {
              credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)
              if credential != nil {
                  disposition = .UseCredential
              }
          }
      }
      return (disposition, credential)
  }
  </code></pre>