91

In my application, I have a UIWebview that loads linkedin auth page for login. When user logs in, cookies saves into the application.

My app has a logout button that is not related to linkedin login. So when user clicks on this button, he logs off from the app. I want that this log off will clear his linkedin cookies also from the app, so that user will log out completely.

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75

5 Answers5

210

According to this question, you can go through each cookie in the "Cookie Jar" and delete them, like so:

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
   [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];
Community
  • 1
  • 1
Sergio Moura
  • 4,888
  • 1
  • 21
  • 38
  • @Felipe Found a temporary way,need to delete cookies in app load if you logout last time. – Ishu Jun 02 '11 at 07:38
  • @Ishu well, yes, that's exactly what I'm doing now, but it doesn't really answer the question on how to REALLY delete cookies forever and not just for the current session – Felipe Brahm Jun 05 '11 at 21:20
  • It deletes cookies permanently. Any requests that are made after could result in the cookies being recreated. Also, if you're wanting to delete cookies only for situations where a user is logging out, then I would recommend only deleting session cookies, since logging out is really just a termination of the session. Otherwise you lose potential settings that a user wishes to persist across sessions. – dsingleton May 18 '12 at 21:11
  • 19
    Be sure to call [[NSUserDefaults standardUserDefaults] synchronize] to make sure the changes are saved to disk. @felipe this is why you are seeing the cookies return on subsequent runs. – Yetanotherjosh Jun 14 '12 at 23:52
  • I tried this but storage return 0 count , I test it on iOS 8 Simulator .So how can i do to delete cookies of facebook on safari . – Nada Gamal Nov 15 '14 at 11:13
8

Just wanted to add some info regarding this.

In OS X 10.9/iOS 7 and later, you can use -resetWithCompletionHandler: to clear the cookies and cache etc. of the whole app from your sharedSession:

Empties all cookies, caches and credential stores, removes disk files, flushes in-progress downloads to disk, and ensures that future requests occur on a new socket.

[[NSURLSession sharedSession] resetWithCompletionHandler:^{
    // Do something once it's done.
}];

The for-In loop with deleteCookie: sounds like modifying while enumerating a collection to me. (Don't know, could be a bad idea?)

Cai
  • 3,609
  • 2
  • 19
  • 39
  • I have used both methods which successfully run, but I am still finding that google is still showing a search history after all of this (not signed in). Any ideas? Thanks – mylogon Mar 23 '17 at 21:18
  • Will this affect any state of Alamofire and my consumption of my Backend REST APIs? Does not Alamofire base its solution on URLSession? – Sajjon Jun 28 '17 at 15:47
1

You could make a function inside the html of the WebView, that cleans the cookies.

If you need the cleaning to be done only once you could trigger this function with a Titanium event, only when the app starts.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
1

Previous answers didn't help me in the case of using MKWebView. So, I found another solution:

func loadAuthUrl(_ url: URL) {
        
        let finally: VoidClosure = { [weak self] in
            guard let self = self else { return }
            let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 60.0)
            self.webView.load(request)
        }
        
        let cookieStore = webView.configuration.websiteDataStore.httpCookieStore
        cookieStore.getAllCookies({ [weak self] cookies in
            guard let _ = self else { return }
            let instagramCookies = cookies.filter({ $0.domain == ".instagram.com" })
            if instagramCookies.isEmpty {
                finally()
            } else {
                DispatchQueue.global().async(execute: {
                    let group = DispatchGroup()
                    for cookie in cookies {
                        group.enter()
                        cookieStore.delete(cookie, completionHandler: { group.leave() })
                    }
                    group.wait()
                    DispatchQueue.main.async(execute: finally)
                })
            }
        })
    }
Riosixx
  • 11
  • 1
0

If anyone is looking for Swift Solution:

    let storage = HTTPCookieStorage.shared
    if let cookies = storage.cookies{
        for cookie in cookies {
             storage.deleteCookie(cookie)
        }
    }
Saleh Enam Shohag
  • 1,039
  • 1
  • 12
  • 23