HTTPCookie.cookies(withResponseHeaderFields:headerFields,for:URL)在iOS 11中返回0

时间:2018-01-08 10:53:17

标签: cookies ios11

我已经努力解决这个问题超过两个小时,但无法理解发生了什么。

我实际上是在我的APP中存储从Server for AuthLogin URL返回的cookie。以下代码适用于iOS版本低于iOS11但在iOS 11 HTTPCookie.cookies(withResponseHeaderFields: responseheaderFields, for: URL)中返回0元素。请添加您的输入以解决这个问题。谢谢。

if response.response?.statusCode == 200 {
                    if
                        let responseheaderFields = response.response?.allHeaderFields as? [String :String],
                        let URL = response.request?.url {

                        let cookieStorage = HTTPCookieStorage.shared
                        cookieStorage.cookieAcceptPolicy =  .always
                        print("policy: \(cookieStorage.cookieAcceptPolicy.rawValue)")

                        // Store session cookie in Keychain
                        let cookies = HTTPCookie.cookies(withResponseHeaderFields: responseheaderFields, for: URL)
                        let cookieToStore = cookies.filter(
                        { $0.name == "MY_COOKIE" }).first
                        Application.sessionCookie = cookieToStore
                        print("Cookie ==> \(cookieToStore!)")

1 个答案:

答案 0 :(得分:0)

也提出了这个问题。我想出了另一种解决方法

func setCookies(dictionary: [String: String]) {
    for (name, value) in dictionary {
        if let cookie = HTTPCookie(properties: [
            .domain: ".xxxx.com",
            .path: "/",
            .name: name,
            .value: value
            ]) {
            HTTPCookieStorage.shared.setCookie(cookie)
        }
    }
}