我正在用餅乾製作iPhone應用程序。在Safari設置中刪除Cookie不會刪除它們。他們在哪裏儲存?是否有可能從另一個UIWebView中讀取它們?UIWebView的cookies存儲在哪裏?
謝謝!
我正在用餅乾製作iPhone應用程序。在Safari設置中刪除Cookie不會刪除它們。他們在哪裏儲存?是否有可能從另一個UIWebView中讀取它們?UIWebView的cookies存儲在哪裏?
謝謝!
您的應用程序在[NSHTTPCookieStorage sharedHTTPCookieStorage]
容器中有自己的「餅乾罐」。
這裏是你怎麼可能在你的應用程序的餅乾罐餅乾快速瀏覽一下:
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [cookieJar cookies]) {
NSLog(@"%@", cookie);
}
有幾種方法可用於過濾和處理。查看訪問cookie的NSHTTPCookieStorage文檔以及訪問各個cookie屬性的NSHTTPCookie文檔。
感謝指針亞歷克斯!爲了補充這一點,我將放入使用Alex的例子創建的「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」 ......
那很有意思。我不知道存在這樣的事情。感謝您指出。 – 2009-04-21 12:34:38
謝謝,亞歷克斯!正是我在找什麼。 – dot 2009-04-21 21:01:54