Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all cookies of UIWebView?

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.

like image 608
Vaibhav Saran Avatar asked Dec 17 '10 14:12

Vaibhav Saran


2 Answers

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]; 
like image 105
Sergio Moura Avatar answered Oct 09 '22 18:10

Sergio Moura


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?)

like image 27
Cai Avatar answered Oct 09 '22 19:10

Cai