3

I need a urlsession that stores cookies in a separate cookieStorage

In the folllowing code the cookieStorage in urlSession is the same as the shares cookieStorage, is it possible to create a separate cookie store

    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()

Same result if I initialize the cookie store using HTTPCookieStorage()

EDIT

I tried creating a cookie store manually and add cookies to it after the request is completed

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

and at the end I end up with nil as cookies

aryaxt
  • 69,636
  • 87
  • 281
  • 421

2 Answers2

0

If you open up the header file for NSHTTPCookieStorage you'll see this documentation (for some reason these details don't appear in the regular documentation).

/*!
    @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

In order to have a valid app group you need to add it following the instructions in Adding an App to an App Group.

I'm guessing that since you don't have the app groups added to your entitlements it's defaulting to NSHTTPCookieStorage.shared.

Guy Kogus
  • 6,960
  • 1
  • 23
  • 28
0

Apparently HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: "groupName") seems to work only on iOS 10+, on iOS 9 it will return nil even if the documentation says @available(iOS 9.0, *) open class func sharedCookieStorage(forGroupContainerIdentifier identifier: String) -> HTTPCookieStorage

You can use this workaround :

let cookies: HTTPCookieStorage
    if #available(iOS 10.0, *) {
        cookies = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: "groupName")
    } else {
        cookies = HTTPCookieStorage.shared
    }
Lilo
  • 2,584
  • 21
  • 16