I have a NSURLSession that calls dataTaskWithRequest in order to send a POST request in this way
func makeRequest(parameters: String, url:String){
var po
I had the same problem: This gets, sets or delete cookies:
func showCookies() {
let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
//println("policy: \(cookieStorage.cookieAcceptPolicy.rawValue)")
let cookies = cookieStorage.cookies as! [NSHTTPCookie]
println("Cookies.count: \(cookies.count)")
for cookie in cookies {
var cookieProperties = [String: AnyObject]()
cookieProperties[NSHTTPCookieName] = cookie.name
cookieProperties[NSHTTPCookieValue] = cookie.value
cookieProperties[NSHTTPCookieDomain] = cookie.domain
cookieProperties[NSHTTPCookiePath] = cookie.path
cookieProperties[NSHTTPCookieVersion] = NSNumber(integer: cookie.version)
cookieProperties[NSHTTPCookieExpires] = cookie.expiresDate
cookieProperties[NSHTTPCookieSecure] = cookie.secure
// Setting a Cookie
if let newCookie = NSHTTPCookie(properties: cookieProperties) {
// Made a copy of cookie (cookie can't be set)
println("Newcookie: \(newCookie)")
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(newCookie)
}
println("ORGcookie: \(cookie)")
}
}
func deleteCookies() {
let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
let cookies = cookieStorage.cookies as! [NSHTTPCookie]
println("Cookies.count: \(cookies.count)")
for cookie in cookies {
println("name: \(cookie.name) value: \(cookie.value)")
NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie)
}
//Create newCookie: You need all properties, because else newCookie will be nil (propertie are then invalid)
var cookieProperties = [String: AnyObject]()
cookieProperties[NSHTTPCookieName] = "locale"
cookieProperties[NSHTTPCookieValue] = "nl_NL"
cookieProperties[NSHTTPCookieDomain] = "www.digitaallogboek.nl"
cookieProperties[NSHTTPCookiePath] = "/"
cookieProperties[NSHTTPCookieVersion] = NSNumber(integer: 0)
cookieProperties[NSHTTPCookieExpires] = NSDate().dateByAddingTimeInterval(31536000)
var newCookie = NSHTTPCookie(properties: cookieProperties)
println("\(newCookie)")
NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(newCookie!)
}