title: Connect to a Server with Invalid Certificate using NSURLSession (swift2,xcode7,ios9)
body: <p>I'm using Xcode 7, Swift 2, and iOS9. I want to connect to a web service using NSURLSession but I get the following error when I try to connect:</p>
  
  <pre><code>2015-10-13 16:07:33.595 XCTRunner[89220:4520715] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
  2015-10-13 16:07:33.604 XCTRunner[89220:4520571] Error with connection, details: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “domainapi.com” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=&lt;SecTrustRef: 0x7fac7b6facc0&gt;, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?,
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>func request( dataPost : String, successHandler: (response: String) -&gt; Void)-&gt; String {
          let destination:String =  "https://domainapi.com:8743/WebService/sendData"
          let request = NSMutableURLRequest(URL: NSURL(string: destination as String)!)
          request.HTTPMethod = "POST"
          let postString = dataPost
          request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
          request.setValue("0", forHTTPHeaderField: "Content-Length")
          request.setValue("application/xml", forHTTPHeaderField: "Content-Type")
          request.setValue("gzip,deflate", forHTTPHeaderField: "Accept-Encoding")
          request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
          NSLog("Body is: %@", request.HTTPBody!)
          NSLog("Request is: %@", request.allHTTPHeaderFields!)
          NSLog("URL is: %@", destination)
  
          let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
              data, response, error in
  
  
              if error != nil {
                  NSLog("Error with connection, details: %@", error!)
                  return
              }
  
              let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
  
              successHandler(response: responseString as String!);
              NSLog("Data received: %@", data!)
  
          }
  
          task.resume()
          return "worked"
      }
      func viewDidLoad() {
          let dataPost : String = "&lt;webservices&gt;xml data sending&lt;/webservices&gt;"
          request(dataPost, successHandler: {
              (response) in
              let text = response
              print(text)
          });
  </code></pre>
  
  <p>I've looked into <code>NSURLAuthenticationChallenge</code> but I can't seem to figure that out with the code I currently have in place. So my question is how can I connect to the server anyway? I've already tried adding the domain to my <code>NSAppTransportSecurity</code> in Info.plist but that did not work. Turning on <code>NSAllowsArbitraryLoads</code> didn't work either. Any help would be appreciated.</p>