WKNavigationResponse allHeaderFields does not contain JSESSIONID on iOS 12

Viewed 335

I use WKWebView to handle login process of my application. I need to sync cookies between WKWebView and HTTCookieStorage.

After each navigation response, I've migrated JSESSIONID cookie from WKWebView to HTTPCookieStorage. My code works on simulator and iOS 11 devices. But on iOS 12 devices, allHeaderFields does not contain JSESSIONID cookie. I cannot see JSESSIONID cookie anywhere on navigationResponse object on iOS 12 device, while the object contains JSESSIONID on iOS 11 devices. Server response to both devices are similar.

Is there any reason and solution for this problem?

public func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
    //Migrate web cookies to native for WKWebView
    if let httpResponse = navigationResponse.response as? HTTPURLResponse {
        if let headerFiles = httpResponse.allHeaderFields as? [String: String] {
            if let url = httpResponse.url {
                let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFiles, for: url)
                if (cookies.count > 0) {
                    HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL: url)
                }
            }
        }
    }
    decisionHandler(.allow)
}
0 Answers
Related