我有HTTPCookiesStorage的子类,但是storeCookies()从不存储cookie,尽管我在方法中调用了super.storeCookies()。
如果我不使用自定义的httpCookiesSession对象覆盖配置,则Session将自动存储cookie。我在这里迷路了。
这是URLSession调用
var configration = URLSessionConfiguration.default
//configration.httpCookieStorage = customHTTPCookiesStorage()
let session = URLSession.init(configuration: configration)
if let url = URL.init(string: "https://google.com"){
session.dataTask(with: url) { sip,sap,su in
print(sip,sap,su)
var x = session.configuration.httpCookieStorage?.cookies
}.resume()
}
这是自定义类
class customHTTPCookiesStorage: HTTPCookieStorage {
override func storeCookies(_ cookies: [HTTPCookie], for task: URLSessionTask) {
super.storeCookies(cookies, for: task)
for cookies in cookies{
session?.session.configuration.httpCookieStorage?.setCookie(cookies)
}
print("This is where cookies are stored")
}
}
更新1 我的目的不是当401中的Http响应状态时将cookie存储在httpcookiesstore中。