首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UIWebView的cookie存储在哪里?

UIWebView的cookie存储在哪里?
EN

Stack Overflow用户
提问于 2009-04-21 07:49:04
回答 3查看 66.7K关注 0票数 96

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

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-04-21 08:08:18

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

下面是如何快速查看应用程序cookie jar中的cookie的方法:

代码语言:javascript
复制
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
   NSLog(@"%@", cookie);
}

有几种方法可用于过滤和操作。查看用于访问cookie的NSHTTPCookieStorage文档,以及用于访问各个cookie属性的NSHTTPCookie文档。

票数 171
EN

Stack Overflow用户

发布于 2011-06-02 00:19:30

谢谢你的指点,Alex!为了补充这一点,我将放入我使用Alex的示例创建的"cookie dumper“。也许这对其他人有帮助。

代码语言:javascript
复制
- (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;
}
票数 21
EN

Stack Overflow用户

发布于 2013-10-05 04:27:56

Alex有一个很好的想法,可以把它归入一个类别。以下是我最终使用的内容:

NSHTTPCookieStorage+Info.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@interface NSHTTPCookieStorage (Info)

+ (NSDictionary*) describeCookies;
+ (NSDictionary *) describeCookie:(NSHTTPCookie *)cookie;

@end

NSHTTPCookieStorage.m

代码语言:javascript
复制
@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"...

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/771498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档