Using HTTPSCookiesStorage can I screen(filter) cookies storage in URLSession?

464 views Asked by At

I have subclassed HTTPCookiesStorage, but the storeCookies() never store the cookies although, i am calling super.storeCookies() within the method.

Session stores the cookies automatically if i dont override the configuration with my custom httpCookiesSession object. I am kind of lost here.

This is the URLSession call

   var configration = URLSessionConfiguration.default
    //configration.httpCookieStorage = customHTTPCookiesStorage()

    let session = URLSession.init(configuration: configration)



    if let url = URL.init(string: "https://google.com"){
        session.dataTask(with: url) { sip,sap,su in
            print(sip,sap,su)

            var x = session.configuration.httpCookieStorage?.cookies

        }.resume()
    }

This is the custom class

class customHTTPCookiesStorage: HTTPCookieStorage {

    override func storeCookies(_ cookies: [HTTPCookie], for task: URLSessionTask) {

        super.storeCookies(cookies, for: task)
        for cookies in cookies{
            session?.session.configuration.httpCookieStorage?.setCookie(cookies)
        }

        print("This is where cookies are stored")
    }
}

Update 1 My aim is not to store the cookies in httpcookiesstore when Http response status in 401

0

There are 0 answers