96
votes

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!

4

4 Answers

171
votes

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.

21
votes

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;
}
3
votes

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"...

1
votes

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