UIWebView的cookie在哪里存储?
我正在用cookiesbuild立一个iPhone应用程序。 在Safari设置中删除cookies不会将其删除。 他们在哪里储存? 是否有可能从另一个UIWebView读取它们?
谢谢!
您的应用程序在[NSHTTPCookieStorage sharedHTTPCookieStorage]
容器中有自己的“cookie jar”。
以下是您可以快速浏览应用程序Cookie jar中的cookie的方法:
NSHTTPCookie *cookie; NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [cookieJar cookies]) { NSLog(@"%@", cookie); }
有几种方法可用于过滤和操作。 查看访问cookie的NSHTTPCookieStorage文档以及访问单个cookie属性的NSHTTPCookie文档。
感谢指针亚历克斯! 为了增加这个function,我将使用Alex创build的“cookie翻车机”。 也许这会帮助别人。
- (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 + 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
使输出多一点“JSON-Y”…