96

I'm building an iPhone app with cookies. Deleting cookies in the Safari settings doesn't delete them. Where are they stored? Is it possible to read them from another UIWebView?

Thanks!

koregan
  • 10,054
  • 4
  • 23
  • 36
dot
  • 2,823
  • 7
  • 38
  • 52

4 Answers4

171

Your application has its own "cookie jar" in the [NSHTTPCookieStorage sharedHTTPCookieStorage] container.

Here's how you might take a quick look at the cookies in your application's cookie jar:

NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
   NSLog(@"%@", cookie);
}

Several methods are available for filtering and manipulation. Take a look at the NSHTTPCookieStorage documentation for accessing cookies, and the NSHTTPCookie documentation for accessing individual cookie properties.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • 1
    That's interesting. I didn't know such a thing existed. Thanks for pointing it out. – Brad Larson Apr 21 '09 at 12:34
  • That's great! How do I log a specific cookie for a specific server? – Lior Frenkel Mar 16 '11 at 07:12
  • @Hamutsi: See the `-cookiesForURL:` instance method in the `NSHTTPCookieStorage` class. Read the documentation link for more info. – Alex Reynolds Apr 28 '11 at 13:25
  • 3
    This method doesn't really work because this will only delete the cookies until you quit the app. But when you reopen it cookies will still be there. – Felipe Brahm May 20 '11 at 22:28
  • To get cookies for a specific server, use the `cookiesForURL` method instead of `cookies` – gyimi Mar 29 '11 at 09:31
  • @AlexReynolds sorry to bug, but this isn't quite a full question for SO yet: can those cookies be accessed (perhaps they just are) by any `NSURLConnection` instance? I need to have a user authenticate with a `UIWebView` first, then I want to switch to Web Services. – Dan Rosenstark Mar 24 '15 at 17:25
  • Yes, they are available and passed along with subsequent requests. However, you will want to check the lifespan between app sessions. – Alex Reynolds Mar 24 '15 at 17:51
  • Can I prevent this "cookie jar" to being create? I mean, I don't want to create any cookies for my UIWebView. Can I do that? – Suryakant Sharma Feb 04 '16 at 06:39
  • I have small question that is, what will happen if i don't remove cookies from webview? Will it affect app size or Will appstore people allow that thing? – Neelam Pursnani Jul 19 '18 at 09:13
  • Here's another question that answers how to clear the UIWebView cache in ObjC and Swift: https://stackoverflow.com/questions/5468553/clearing-uiwebview-cache – Alex Reynolds Jul 19 '18 at 22:10
21

Thanks for the pointer Alex! To add to this I will drop in my "cookie dumper" that I created using Alex's example. Maybe this will help someone else.

- (void) dumpCookies:(NSString *)msgOrNil {
NSMutableString *cookieDescs    = [[[NSMutableString alloc] init] autorelease];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
    [cookieDescs appendString:[self cookieDescription:cookie]];
}
NSLog(@"------ [Cookie Dump: %@] ---------\n%@", msgOrNil, cookieDescs);
NSLog(@"----------------------------------");
}

- (NSString *) cookieDescription:(NSHTTPCookie *)cookie {

NSMutableString *cDesc      = [[[NSMutableString alloc] init] autorelease];
[cDesc appendString:@"[NSHTTPCookie]\n"];
[cDesc appendFormat:@"  name            = %@\n",            [[cookie name] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[cDesc appendFormat:@"  value           = %@\n",            [[cookie value] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[cDesc appendFormat:@"  domain          = %@\n",            [cookie domain]];
[cDesc appendFormat:@"  path            = %@\n",            [cookie path]];
[cDesc appendFormat:@"  expiresDate     = %@\n",            [cookie expiresDate]];
[cDesc appendFormat:@"  sessionOnly     = %d\n",            [cookie isSessionOnly]];
[cDesc appendFormat:@"  secure          = %d\n",            [cookie isSecure]];
[cDesc appendFormat:@"  comment         = %@\n",            [cookie comment]];
[cDesc appendFormat:@"  commentURL      = %@\n",            [cookie commentURL]];
[cDesc appendFormat:@"  version         = %d\n",            [cookie version]];

//  [cDesc appendFormat:@"  portList        = %@\n",            [cookie portList]];
//  [cDesc appendFormat:@"  properties      = %@\n",            [cookie properties]];

return cDesc;
}
bladnman
  • 2,591
  • 1
  • 25
  • 20
  • You might even add this as a category to extend `NSHTTPCookieStorage`: http://macdevelopertips.com/objective-c/objective-c-categories.html – Alex Reynolds Sep 13 '11 at 19:52
  • +1 for extracting out the logic of how to display a cookie into a separate method! Even for such a small snippet, it helps!! – Guven Feb 13 '12 at 02:36
  • Thank you! There is a typo: [cookie version] is NSUInteger, so %d should be used. – Pavel Alexeev Apr 20 '13 at 15:32
3

Alex had a great idea about putting this in a category. Here's what I ended up using:

NSHTTPCookieStorage+Info.h

#import <Foundation/Foundation.h>

@interface NSHTTPCookieStorage (Info)

+ (NSDictionary*) describeCookies;
+ (NSDictionary *) describeCookie:(NSHTTPCookie *)cookie;

@end

NSHTTPCookieStorage.m

@implementation NSHTTPCookieStorage (Info)

+ (NSDictionary*) describeCookies {
    NSMutableDictionary *descriptions = [NSMutableDictionary new];

    [[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] enumerateObjectsUsingBlock:^(NSHTTPCookie* obj, NSUInteger idx, BOOL *stop) {
        [descriptions setObject:[[self class] describeCookie:obj] forKey:[[obj name] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }];

    NSLog(@"Cookies:\n\n%@", descriptions);
    return descriptions;
}

+ (NSDictionary *) describeCookie:(NSHTTPCookie *)cookie {
    return @{@"value" : [[cookie value] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
         @"domain" : [cookie domain] ? [cookie domain]  : @"n/a",
         @"path" : [cookie path] ? [cookie path] : @"n/a",
         @"expiresDate" : [cookie expiresDate] ? [cookie expiresDate] : @"n/a",
         @"sessionOnly" : [cookie isSessionOnly] ? @1 : @0,
         @"secure" : [cookie isSecure] ? @1 : @0,
         @"comment" : [cookie comment] ? [cookie comment] : @"n/a",
         @"commentURL" : [cookie commentURL] ? [cookie commentURL] : @"n/a",
         @"version" : @([cookie version]) };

}

@end

Makes the output a bit more "JSON-y"...

DarkestOne
  • 66
  • 3
1

in sandbox:Library->Cookies->Cookies.binarycookies but you can not open the .binarycookies directly, you can run a script:

  1. Download and install Python

  2. Download BinaryCookieReader.py

  3. Run "Python BinaryCookieReader.py" on the terminal

enter image description here

as you can see, output log contains detail cookies description

Adeel
  • 2,901
  • 7
  • 24
  • 34
xy z
  • 11
  • 2