如何在标题响应navigationAction.request.allHTTPHeaderFields中处理多个cookie?[" Set-Cookie"]

时间:2016-10-18 21:03:16

标签: ios swift cookies

swift如何处理navigationAction.request.allHTTPHeaderFields?[" Set-Cookie"]。我有多个cookie的响应,我想知道如何处理它,因为navigationAction.request.allHTTPHeaderFields?[" Set-Cookie"]返回一个字符串。我根本无法测试这个,因为我正在尝试实现单点登录,我必须等到它被部署才能看到它是否有效。

1 个答案:

答案 0 :(得分:0)

A better solution for future users just in case is to implement WKNavigationDelegate and iterate through the cookies like so:

func webView(_: WKWebView, decidePolicyFor: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

    let response = (decidePolicyFor.response as! HTTPURLResponse)
    let cookies = HTTPCookie.cookies(withResponseHeaderFields: response.allHeaderFields as! [String : String], for: response.url!)

    for cookie in cookies {
       //cookie.value, cookie.name etc... do logic here
    }


    decisionHandler(.allow)
 }