session.dataTaskWithRequest wrong URL

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

UPDATE:


In the playground, my sample code worked as well as expected. The passed URL http:// remains http://. It seems to go wrong only in the Xcode app which http:// is replaced by https: //.


Regards,

Rob

This is a new "security" feature. iOS 9 enforces secure connections, so all normal "http" connections will fail by default. Right now this won't work for many use cases, so you can opt-out for specific domains or even completely. Look for the keyword "App Transport Security" (ATS).

You'll find some more information in this thread:

https://forums.developer.apple.com/message/5857

session.dataTaskWithRequest wrong URL
 
 
Q