5

How to delete cookies in UIWebView? This code:

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

deletes cookies but when I restart my application, there are same cookies in NSHTTPCookieStorage. Sometimes this code works, but i want to make it work everytime.How to solve this problem?

Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109

5 Answers5

7

This worked for me:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *allCookies = [cookies cookies];

for(NSHTTPCookie *cookie in allCookies) {
    if([[cookie domain] rangeOfString:@"facebook.com"].location != NSNotFound) {
        [cookies deleteCookie:cookie];
    }
}
Benjamin Oman
  • 1,654
  • 1
  • 17
  • 19
7

Try something like this:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSArray* facebookCookies = [cookies cookiesForURL:
                                    [NSURL URLWithString:@"http://login.facebook.com"]];
        for (NSHTTPCookie* cookie in facebookCookies) {
            [cookies deleteCookie:cookie];
        }
LightNight
  • 1,616
  • 6
  • 30
  • 59
  • You said that code working sometimes. Can you say more about it? – LightNight Sep 27 '11 at 12:42
  • I mean it works random, i can't controll this:) In same questions people write about some lag in ios 4.0++. Cookies deleting while your app is launching, if app has been restarted, cookies return) – Timur Mustafaev Sep 27 '11 at 13:50
  • 1
    Cookie saved in NSUserDefaults, so to clean them you can remove all your NSUserDefaults data, like this: `NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];` – LightNight Sep 27 '11 at 14:30
3

deleting a single cookie does not always work for some odd reason. To truly get a cookie deleted you will need to store the not specific cookie and then reload them and then iterate through all cookies and delete them such as this

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
   if (cookies != nil && cookies.count > 0) {
       for (NSHTTPCookie *cookie in cookies) {
           [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
       }
       [[NSUserDefaults standardUserDefaults] synchronize];
   }
Tony
  • 4,609
  • 2
  • 22
  • 32
0

Make sure to call:

[[NSUserDefaults standardUserDefaults] synchronize];

in the end... Works like a charm...

Doug Amos
  • 4,243
  • 1
  • 22
  • 23
Naldo Lopes
  • 1,191
  • 2
  • 11
  • 13
0

The similar for previous one but simplier:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSArray *allCookies = [cookies cookies];

for(NSHTTPCookie *cookie in allCookies) {
    if([[cookie domain] contains:@"facebook.com"]) {
        [cookies deleteCookie:cookie];
    }
}

The "best answer" is bad because it allows to delete cookies for the specified concrete URLs. So for example you delete cookie for "login.facebook.com" but you may miss "www.login.facebook.com"

Vyachaslav Gerchicov
  • 2,317
  • 3
  • 23
  • 49