我对 HTTPCookiesStorage 进行了子类化,但是 storeCookies() 从不存储 cookie,尽管我在方法中调用 super.storeCookies()。
如果我不使用我的自定义 httpCookiesSession 对象覆盖配置,会话会自动存储 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 我的目标是当 Http 响应状态为 401 时,不将 cookie 存储在 httpcookiesstore 中