12

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)

Andrew Johnson
  • 579
  • 4
  • 23

3 Answers3

16

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

HTTPCookieStorage.shared.cookies?.forEach(HTTPCookieStorage.shared.deleteCookie)
jwswart
  • 1,226
  • 14
  • 16
14
let cookieStore = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie in cookieStore.cookies ?? [] {
    cookieStore.deleteCookie(cookie)
}
smottt
  • 3,272
  • 11
  • 37
  • 44
Andrew Johnson
  • 579
  • 4
  • 23
9

Similarly, for swift 3.0+, xCode 8.0+

let cookieStore = HTTPCookieStorage.shared
for cookie in cookieStore.cookies ?? [] {
    cookieStore.deleteCookie(cookie)
}
Rujoota Shah
  • 1,251
  • 16
  • 18