在WKWebView和HTTPCookieStorage之间复制cookie - 找不到302个cookie

时间:2016-09-15 18:35:03

标签: ios cookies ios9 wkwebview ios10

我们正在将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?

1 个答案:

答案 0 :(得分:0)

尝试使用PWKWebView替换原来的WKWebView