title: How do I perform an insecure URLSession query in iOS
body: <p>I'm trying to perform a query to a website that I run. However, currently, this website's certificate is invalid (for a valid reason for now).</p>
  
  <p>I'm trying to query it with this code:</p>
  
  <pre><code>private static func performQuery(_ urlString: String) {
      guard let url = URL(string: urlString) else {
          return
      }
      print(url)
      URLSession.shared.dataTask(with: url) {
          (data, response, error) in
          if error != nil {
              print(error!.localizedDescription)
          }
          guard let data = data else {
              return
          }
          do {
              let productDetails = try JSONDecoder().decode([ProductDetails].self, from: data)
              DispatchQueue.main.async {
                  print(productDetails)
              }
          } catch let jsonError {
              print(jsonError)
          }
      }.resume()
  }
  </code></pre>
  
  <p>However, I get:</p>
  
  <pre><code>NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
  The certificate for this server is invalid. You might be connecting to a server that is pretending to be “mydomain.com” which could put your confidential information at risk.
  </code></pre>
  
  <p>How can I make an insecure URLSession query (equivalent to -k in CURL)?</p>
  
  <p>I've tried setting these:</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;mydomain.com&lt;/key&gt;
          &lt;dict&gt;
              &lt;key&gt;NSExceptionAllowsInsecureHTTPLoads&lt;/key&gt;
              &lt;true/&gt;
              &lt;key&gt;NSIncludesSubdomains&lt;/key&gt;
              &lt;true/&gt;
              &lt;key&gt;NSTemporaryExceptionRequiresForwardSecrecy&lt;/key&gt;
              &lt;false/&gt;
              &lt;key&gt;NSThirdPartyExceptionAllowsInsecureHTTPLoads&lt;/key&gt;
              &lt;true/&gt;
          &lt;/dict&gt;
      &lt;/dict&gt;
  &lt;/dict&gt;
  </code></pre>
  
  <p>And yes I don't intend to release this to the App Store with insecure access, but I need to get the code tested and right now we can't get a valid certificate, so this is purely for development purposes.</p>