我们正在将Cookie从WKWebView复制到HTTPCookitStorage.shared以供URLSession任务使用。
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
if let response = navigationResponse.response as? HTTPURLResponse {
var headers = [String : String]()
for (header, value) in response.allHeaderFields {
headers[(header as? String)!] = value as? String
}
let cookies = HTTPCookie.cookies(withResponseHeaderFields: headers, for: response.url!)
for cookie: HTTPCookie in cookies {
print(cookie)
HTTPCookieStorage.shared.setCookie(cookie)
}
}
decisionHandler(.allow)
}
捕获大多数cookie,但是如果WKWebView中发生302重定向则不会。是否有委托方法或其他技术可以从具有302响应代码的响应中捕获cookie?