9

I have a UIWebView, and I don't want it to store an cookies, so just before the webview is loaded I do:

NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *cookie in cookies) {
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}

Checking the count of cookies is 0 so they are all deleted. But when I go to stackoverflow it still recognizes my Google Account and logs me in. How does this happen? I though it worked with cookies?

Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
Jonathan.
  • 53,997
  • 54
  • 186
  • 290

3 Answers3

7

I had to deal with the exact same issue and I found 2 ways to deal with that problem. I first noticed that cookies get (sometimes) set at weird times (Strange behaviour especially with ios 4.0).

  • Instant deletion of cookies after the user was on a webview often didn't get me the expected results.

I then integrated a persistant, manual flag that was set True on a "logout" (aka clear all cookies / delete other user data) action. upon the next login (aka user-login-based action) I cleared the cookies again (the same way you did it in your code-post).

Later I found out, that listening to NSHTTPCookieManagerCookiesChangedNotification and then deleting cookies worked really well too.

hope I could help.

samsam
  • 3,125
  • 24
  • 40
6

Try changing the cookie acceptance policy instead:

[NSHTTPCookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
defbyte
  • 611
  • 6
  • 14
  • The problem is this stops cookies from being made I want to remove existing ones. Ideally clear all caches, cookies, local storage and other user data. – Jonathan. Jul 12 '11 at 00:00
  • Hi Jonathan, based on your question I did not know what additional actions you wanted to perform. Looks like you are looking for a full cache clear - not sure that is possible off hand. What exactly are you trying to do? Why do you need to wipe all caches, cookies, and local storage / user data? – defbyte Jul 18 '11 at 20:12
0

Use following and it will work..

   NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
   NSHTTPCookie *cookie;
   for(cookie in [storage cookies])
    {
      NSLog(@"cookie to be deleted:%@", cookie);
      [storage deleteCookie:cookie];
    }
   [[NSUserDefaults standardUserDefaults] synchronize];

here don't miss last line [[NSUserDefaults standardUserDefaults] synchronize]; otherwise you will remain puzzle.

RamjanSayyad
  • 45
  • 10