title: iOS first app, accept untrusted certificate for hello world
body: <p>I am making a basic "hello world" iOS app.  I have an ubuntu server in the cloud which I want to query from the iOS app.  I understand that the server needs to be secure, ie, needs to be accessed via https request, and the certificate on the server needs to be "trusted".  </p>
  
  <p>Right now what I am trying to do is override this requirement.  I have a self-signed certificate that is working for https on my server.  When I make the request with the iOS app, it gives me some errors about <code>NSURLErrorFailingURLPeerTrustErrorKey</code> and even one line returned saying: <code>NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?</code>.</p>
  
  <p>I know this is a common issue and there are many threads on this site about how to deal with it.  I tried a piece of code from <a href="https://stackoverflow.com/questions/19507207/how-do-i-accept-a-self-signed-ssl-certificate-using-ios-7s-nsurlsession-and-its">this post</a>.  I added this piece to my code:</p>
  
  <pre><code>    func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -&gt; Void) {
          if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust{
              let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
              completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential);
          }
      }
  </code></pre>
  
  <p>My entire ViewController code is here:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UITextFieldDelegate, NSURLSessionDelegate {
      // MARK: Properties
      @IBOutlet weak var nameTextField: UITextField!
      @IBOutlet weak var mealNameLabel: UILabel!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Handle the text field’s user input through delegate callbacks.
          nameTextField.delegate = self
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      // MARK: UITextFieldDelegate
      func textFieldShouldReturn(textField: UITextField) -&gt; Bool {
          // Hide the keyboard.
          textField.resignFirstResponder()
          return true
      }
  
      func textFieldDidEndEditing(textField: UITextField) {
          mealNameLabel.text = nameTextField.text
      }
  
      // MARK: Actions
      @IBAction func setDefaultLabelText(sender: UIButton) {
          mealNameLabel.text = "Default Text"
          post_request()
      }
  
      func post_request(){
          let request = NSMutableURLRequest(URL: NSURL(string: "https://54.164.XXX.XX/post_script.php")!)
          request.HTTPMethod = "POST"
          let postString = "id=13&amp;name=Jack"
          request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
  
          let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
              data, response, error in
  
              if error != nil {
                  print("error=\(error)")
                  return
              }
  
              print("response = \(response)")
  
              let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
              print("responseString = \(responseString)")
          }
          task.resume()
      }
  
      func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -&gt; Void) {
          if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust{
              let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
              completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential);
          }
      }
  
  }
  </code></pre>
  
  <p>It looks like the idea is to pick up the <code>challenge</code> event from the connection, and tell it that "yes I want to proceed".  But that piece of code does not seem to be getting called.  The post request gets sent, but I receive the error messages about the untrusted certificate.  Can someone help me fix my code so that I can accept this certificate from my server?</p>