【问题标题】:URLSession to use a custom CookieStorage on iOS?URLSession 在 iOS 上使用自定义 CookieStorage?
【发布时间】:2018-03-31 23:56:57
【问题描述】:

我需要一个 urlsession 将 cookie 存储在单独的 cookieStorage 中

在下面的代码中urlSession中的cookieStorage和共享的cookieStorage是一样的,是否可以创建一个单独的cookie存储

    let config = URLSessionConfiguration.default
    session = URLSession(configuration: config)
    config.httpCookieAcceptPolicy = .always
    session.configuration.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: "adfadf")

    let task = session.dataTask(with: URL(string: "https://www.google.com")!) { (data, response, error) in
        print((response as? HTTPURLResponse)?.allHeaderFields ?? "")

        DispatchQueue.main.async {
            print(self.session.configuration.httpCookieStorage?.cookies ?? "wtf")
            print(HTTPCookieStorage.shared === self.session.configuration.httpCookieStorage)
        }
    }

    task.resume()

如果我使用 HTTPCookieStorage() 初始化 cookie 存储,结果相同

编辑

我尝试手动创建一个 cookie 存储,并在请求完成后向其中添加 cookie

let cookies = HTTPCookie.cookies(withResponseHeaderFields: headers, for: url)
 // cookies is not empty
self.cookieStore.setCookies(cookies, for: url, mainDocumentURL: nil)
print(self.cookieStore.cookies) //result is nil

最后我得到 nil 作为 cookie

【问题讨论】:

    标签: ios swift urlsession nshttpcookiestorage


    【解决方案1】:

    如果您打开 NSHTTPCookieStorage 的头文件,您将看到此文档(由于某些原因,这些详细信息不会出现在常规文档中)。

    /*!
        @method sharedCookieStorageForGroupContainerIdentifier:
        @abstract Get the cookie storage for the container associated with the specified application group identifier
        @param identifier The application group identifier
        @result A cookie storage with a persistent store in the application group container
        @discussion By default, applications and associated app extensions have different data containers, which means
        that the sharedHTTPCookieStorage singleton will refer to different persistent cookie stores in an application and
        any app extensions that it contains. This method allows clients to create a persistent cookie storage that can be
        shared among all applications and extensions with access to the same application group. Subsequent calls to this
        method with the same identifier will return the same cookie storage instance.
     */
    @available(iOS 9.0, *)
    open class func sharedCookieStorage(forGroupContainerIdentifier identifier: String) -> HTTPCookieStorage
    

    要获得有效的应用组,您需要按照Adding an App to an App Group 中的说明添加它。

    我猜由于您没有将应用组添加到您的权利中,因此默认为NSHTTPCookieStorage.shared

    【讨论】:

    • 最后有帮助吗?
    【解决方案2】:

    显然HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: "groupName") 似乎只适用于 iOS 10+,在 iOS 9 上即使文档显示 @available(iOS 9.0, *) open class func sharedCookieStorage(forGroupContainerIdentifier identifier: String) -> HTTPCookieStorage 也会返回 nil

    您可以使用此解决方法:

    let cookies: HTTPCookieStorage
        if #available(iOS 10.0, *) {
            cookies = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: "groupName")
        } else {
            cookies = HTTPCookieStorage.shared
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2018-11-02
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      相关资源
      最近更新 更多