I've been developing using HTTP. The code below works great when connecting with the development server using HTTP. However, when I change the scheme to https, it doesn't send a successful https post to the server.
What else do I need to do to switch from HTTP POST to HTTPS POST?
class func loginRemote(successHandler:()->(), errorHandler:(String)->()) {
let user = User.sharedInstance
// this is where I've been changing the scheme to https
url = NSURL(String: "http://url.to/login.page")
let request = NSMutableURLRequest(URL: url)
let bodyData = "email=\(user.email)&password=\(user.password)"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
request.HTTPMethod = "POST"
let session = NSURLSession.sharedSession()
// posting login request
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
// email+password were good
successHandler()
} else {
// email+password were bad
errorHandler("Status: \(httpResponse.statusCode) and Response: \(httpResponse)")
}
} else {
NSLog("Unwrapping NSHTTPResponse failed")
}
})
task.resume()
}
You will have to implement one of the
NSURLSessionDelegate
methods so that it will accept the SSL certificate.WARNING: This will blindly accept any SSL certificate/connection that you attempt. This is not a safe practice, but it will allow you to test your server using HTTPS.
UPDATE: Swift 4+