title: Swift 3 Connect to server with invalid certificate for XML
body: <p>I'm having some issues with a connection to a server. I want to simply send a username and password and get some data from an XML file.  </p>
  
  <pre><code>    let loginString = String(format: "%@:%@", username, password)
      let loginData = loginString.data(using: String.Encoding.utf8)!
      let base64LoginString = loginData.base64EncodedString()
  
      let baseUrl = "xxxxxx"
      let request = NSMutableURLRequest(url: NSURL(string: baseUrl)! as URL)
      request.httpMethod = "POST"
      request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
      let session = URLSession.shared
      request.httpMethod = "GET"
  
      var err: NSError?
  
      let task = session.dataTask(with: request as URLRequest) {
          (data, response, error) in
  
          if data == nil {
              print("dataTaskWithRequest error: \(error)")
              return
          }
  
          let xml = SWXMLHash.parse(data!)
          print(xml["mobile_devices"]["mobile_device"]["serial_number"].element?.text)
  
          DispatchQueue.main.async(execute: {
              // use main thread for UI updates
          })
  
      }
      task.resume()
  </code></pre>
  
  <p>When I run it, I get the following error(s): </p>
  
  <blockquote>
    <p>nw_coretls_callback_handshake_message_block_invoke_3 tls_handshake_continue: [-9812]
    2017-05-04 08:20:45.311 xxxx[23410:4428038] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)</p>
    
    <p>NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
    dataTaskWithRequest error: Optional(Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xxxxxxx” which could put your confidential information at risk." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9813, NSErrorPeerCertificateChainKey=("cert(0x7f88d8834000) s: xxxxxxx JSS Built-in Certificate Authority>",</p>
  </blockquote>
  
  <p>I've never really done anything with APIs and things before, so I'm learning as I go. Previously I had to edit the plist file to allow the connection, and that fixed a problem I had before. I've got this far but I'm not sure how to get past it. </p>
  
  <p>I saw this: <a href="https://stackoverflow.com/questions/33128240/connect-to-a-server-with-invalid-certificate-using-nsurlsession-swift2-xcode7-i">Connect to a Server with Invalid Certificate using NSURLSession (swift2,xcode7,ios9)</a>
   but it broke the </p>
  
  <pre><code>let session = URLSession.shared 
  </code></pre>
  
  <p>line in my code.</p>
  
  <p>I've seen this as well, but I'm unsure how to use it:</p>
  
  <p><a href="https://stackoverflow.com/questions/24063927/swift-how-to-request-a-url-with-a-self-signed-certificate">Swift: How to request a URL with a self-signed certificate?</a></p>