These are my two examples :
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = ["Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": UIDevice.currentDevice().model]
var request = NSMutableURLRequest(URL: NSURL(string: "http://XXX"))
request.HTTPMethod = "POST"
let valuesToSend = ["key":value, "key2":value]
var error: NSError?
let data = NSJSONSerialization.dataWithJSONObject(valuesToSend, options:NSJSONWritingOptions.PrettyPrinted, error: &error)
request.HTTPBody = data
if error == nil {
let task = NSURLSession(configuration: config).dataTaskWithRequest(request,
completionHandler: {data, response, error in
if error == nil {
println("received == \(NSString(data: data, encoding: NSUTF8StringEncoding))")
}
})
task.resume()
} else {
println("Oups error \(error)")
}
AND the second
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
config.HTTPAdditionalHeaders = ["Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": UIDevice.currentDevice().model]
var request = NSMutableURLRequest(URL: NSURL(string: "http://XXX"))
request.HTTPMethod = "POST"
let valuesToSend = ["key":value, "key2":value]
var error: NSError?
let data = NSJSONSerialization.dataWithJSONObject(valuesToSend, options:NSJSONWritingOptions.PrettyPrinted, error: &error)
if error == nil {
let task = NSURLSession(configuration: config).uploadTaskWithRequest(request, fromData: data,
completionHandler: {data, response, error in
if error == nil {
println("received == \(NSString(data: data, encoding: NSUTF8StringEncoding))")
}
})
task.resume()
} else {
println("Oups error \(error)")
}
So I wonder : what are the differences between these twos examples and what about the better for my case ( simple post and reception )
The two are in background no ? So ?