我正在构建一个带有cookie的iPhone应用程序.在Safari设置中删除Cookie不会删除它们.它们存放在哪里?是否可以从另一个UIWebView读取它们?
谢谢!
您的应用程序在[NSHTTPCookieStorage sharedHTTPCookieStorage]
容器中有自己的"cookie jar" .
以下是您可以快速查看应用程序cookie jar中的cookie的方法:
NSHTTPCookie *cookie; NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [cookieJar cookies]) { NSLog(@"%@", cookie); }
有几种方法可用于过滤和操作.看看在NSHTTPCookieStorage文档访问饼干和NSHTTPCookie访问各个Cookie属性的文件.
感谢Alex指针!为了补充这一点,我将放入我使用Alex的示例创建的"cookie dumper".也许这会帮助别人.
- (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; }