Where are an UIWebView's cookies stored?
Asked Answered
N

4

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!

Northcliffe answered 21/4, 2009 at 7:49 Comment(0)
H
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.

Humpage answered 21/4, 2009 at 8:8 Comment(10)
That's interesting. I didn't know such a thing existed. Thanks for pointing it out.Lattimore
That's great! How do I log a specific cookie for a specific server?Forseti
To get cookies for a specific server, use the cookiesForURL method instead of cookiesPolyhydroxy
@Hamutsi: See the -cookiesForURL: instance method in the NSHTTPCookieStorage class. Read the documentation link for more info.Humpage
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.Belk
@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.Graces
Yes, they are available and passed along with subsequent requests. However, you will want to check the lifespan between app sessions.Humpage
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?Sailesh
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?Arise
Here's another question that answers how to clear the UIWebView cache in ObjC and Swift: https://mcmap.net/q/219298/-clearing-uiwebview-cacheHumpage
P
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;
}
Parnassus answered 1/6, 2011 at 16:19 Comment(3)
You might even add this as a category to extend NSHTTPCookieStorage: macdevelopertips.com/objective-c/objective-c-categories.htmlHumpage
+1 for extracting out the logic of how to display a cookie into a separate method! Even for such a small snippet, it helps!!Interplanetary
Thank you! There is a typo: [cookie version] is NSUInteger, so %d should be used.Maroon
T
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"...

Traipse answered 4/10, 2013 at 20:27 Comment(0)
W
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

Wendt answered 19/9, 2018 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.