hi,
I'm converting my app to Swift 2. I found something different in session.dataTaskWithRequest. In the code below I use an HTTP URL to post the request. But this is somewhere converted to a HTTPS URL. So the server isn't reachable because the server don't use SSL? Why is this happening? Could I change this to HTTP anyway?
func post(params : NSDictionary, url : String, postCompleted : (succeeded: Bool, msg: String, json: NSDictionary?) -> ()) {
let request = NSMutableURLRequest(URL: NSURL(string: url)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: [])
} catch let error as NSError {
print("Error in request post: \(error)")
request.HTTPBody = nil
} catch {
print("Catch all error: \(error)")
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error in
if error != nil {
if error!.domain == NSURLErrorDomain && error!.code == NSURLErrorTimedOut {
print("Timeout error in post dataTaskWithRequest: \(error!.localizedDescription)")
} else {
print("Error in post dataTaskWithRequest: \(error!.localizedDescription)")
}
postCompleted(succeeded: false, msg: "\(error!.localizedDescription)", json: nil)
} else {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as! NSDictionary
postCompleted(succeeded: true, msg: "Logged in.", json: json)
} catch let error as NSError {
print("Error in request post JSONObjectWithDATA: \(error)")
//request.HTTPBody = nil
} catch {
print("Catch all error: \(error)")
}
}
})
task!.resume()
}
Best Regards,
Rob