How to get cookie from a NSURLSession with Swift?
The Swift rendition might look something like:
let task = session.dataTask(with: request) { data, response, error in
guard
let url = response?.url,
let httpResponse = response as? HTTPURLResponse,
let fields = httpResponse.allHeaderFields as? [String: String]
else { return }
let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields, for: url)
HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL: nil)
for cookie in cookies {
var cookieProperties = [HTTPCookiePropertyKey: Any]()
cookieProperties[.name] = cookie.name
cookieProperties[.value] = cookie.value
cookieProperties[.domain] = cookie.domain
cookieProperties[.path] = cookie.path
cookieProperties[.version] = cookie.version
cookieProperties[.expires] = Date().addingTimeInterval(31536000)
let newCookie = HTTPCookie(properties: cookieProperties)
HTTPCookieStorage.shared.setCookie(newCookie!)
print("name: \(cookie.name) value: \(cookie.value)")
}
}
task.resume()
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!)
}