UIWebView的cookie存储在哪里?


97

我正在使用Cookie构建iPhone应用程序。在Safari设置中删除Cookie不会将其删除。它们存储在哪里?是否可以从另一个UIWebView读取它们?

谢谢!

Answers:


171

您的应用程序在[NSHTTPCookieStorage sharedHTTPCookieStorage]容器中有其自己的“ cookie jar” 。

您可以通过以下方法快速查看应用程序的Cookie罐中的Cookie:

NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
   NSLog(@"%@", cookie);
}

有几种方法可用于过滤和操作。看看在NSHTTPCookieStorage文档访问饼干和NSHTTPCookie访问各个Cookie属性的文件。


1
那很有意思。我不知道这样的事情存在。感谢您指出。
布拉德·拉尔森

谢谢,亚历克斯!正是我想要的。

那很棒!如何记录特定服务器的特定Cookie?
Lior Frenkel

要获取特定服务器的Cookie,请使用cookiesForURL方法代替cookies
gyimi 2011年

3
该方法实际上并不起作用,因为这只会删除cookie,直到您退出应用程序为止。但是当您重新打开它时,cookie仍然会存在。
菲利普·勃拉姆

21

感谢指针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;
}

您甚至可以将此类别添加为扩展类别NSHTTPCookieStoragemacdevelopertips.com/objective-c/objective-c-categories.html
Alex Reynolds

+1,用于提取如何将Cookie显示为单独方法的逻辑!即使是这么小的片段,它也有帮助!!
Guven 2012年

谢谢!有一个错字:[cookie版本]是NSUInteger,因此应使用%d。
Pavel Alexeev 2013年

3

亚历克斯有一个很好的想法,将其归为一类。这是我最终使用的内容:

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


1

sandbox:Library->Cookies->Cookies.binarycookies 但不能.binarycookie直接打开s的情况下,可以运行脚本:

  1. 下载并安装Python

  2. 下载BinaryCookieReader.py

  3. 在终端上运行“ Python BinaryCookieReader.py”

在此处输入图片说明

如您所见,输出日志包含详细的Cookie描述

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.