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!
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!
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.
cookiesForURL
method instead of cookies
–
Polyhydroxy -cookiesForURL:
instance method in the NSHTTPCookieStorage
class. Read the documentation link for more info. –
Humpage NSURLConnection
instance? I need to have a user authenticate with a UIWebView
first, then I want to switch to Web Services. –
Graces 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;
}
NSHTTPCookieStorage
: macdevelopertips.com/objective-c/objective-c-categories.html –
Humpage 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"...
in sandbox:Library->Cookies->Cookies.binarycookies
but you can not open the .binarycookie
s directly, you can run a script:
Download and install Python
Download BinaryCookieReader.py
Run "Python BinaryCookieReader.py" on the terminal
as you can see, output log contains detail cookies description
© 2022 - 2024 — McMap. All rights reserved.