Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erase all cookies in NSURLSession iOS

Tags:

nsurlsession

How can I erase all cookies from the NSHTTPCookieStorage.sharedHTTPCookieStorage? The only methods I am aware of delete a single specified cookie, however, the cookies are handled behind the scenes by NSURLSession. (Programming in Swift)

like image 265
Andrew Johnson Avatar asked Mar 16 '16 13:03

Andrew Johnson


2 Answers

This achieves the same result as the other answers but with one line of code:

HTTPCookieStorage.shared.cookies?.forEach(HTTPCookieStorage.shared.deleteCookie)
like image 78
jwswart Avatar answered Jan 01 '23 09:01

jwswart


let cookieStore = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in cookieStore.cookies ?? [] {
    cookieStore.deleteCookie(cookie)
}
like image 20
Andrew Johnson Avatar answered Jan 01 '23 08:01

Andrew Johnson