We are copying cookies from WKWebView to HTTPCookitStorage.shared to be used by URLSession tasks.
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)
}
That captures most cookies, but not if there was a 302 redirect happening in the WKWebView. Is there a delegate method or some other technique that I can do to capture the cookies from a response that has a 302 response code?