17

I need the user to be able to log out. When they log in now, a cookie gets saved automatically, that works fine. But I want to clear all cookies.

NSURLCache.sharedURLCache().removeAllCachedResponses()

This does not work

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
alvarlagerlof
  • 1,516
  • 1
  • 18
  • 27

1 Answers1

37

You can remove all the cookies specifically stored for an URL like this (Swift 3):

let cstorage = HTTPCookieStorage.shared
if let cookies = cstorage.cookies(for: url) {
    for cookie in cookies {
        cstorage.deleteCookie(cookie)
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 1
    There is also simpler method to remove all cookies for all urls: `HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)` – brigadir Jul 20 '22 at 20:50