Copying cookies from one URLSession to another

I have a URLSession.shared request that will GET and the cookie is properly set. I can see in HTTPCookieStorage.shared.cookies(for: self.url) that the value is available. However, there is a different URLSession with its own configuration that is making a websocket request later. I cannot reuse the same session instance however I do have access to the task instance. I tried HTTPCookieStorage.storeCookies(cookies, for: task) but XCode is telling me that storeCookies is an instance function, not a class function. How can I get access to the specific cookie storage for the session or task instance of this additional request?

How can I get access to the specific cookie storage for the session … ?

If you have access to the session then you can get the cookie store from its configuration. For example:

let session: URLSession = …
let cookieStoreQ = session.configuration.httpCookieStorage

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Terms of Service

Privacy Policy

Cookie Policy