Re: Delay in NSHTTPCookieStorage
Re: Delay in NSHTTPCookieStorage
- Subject: Re: Delay in NSHTTPCookieStorage
- From: Jerry Krinock <email@hidden>
- Date: Sun, 1 Apr 2007 17:44:57 -0700
I did some more experiments on this. I found that the solution I
proposed at the end of my last post does not work because, although
other apps don't get the cookies changed notification for 5 seconds
or so, the app which handled the HTTP message which changed the
cookie gets the notification immediately. Furthermore, I found that
this same 5-second delay applies to the cookies returned by
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies].
In other words, even after you delete a cookie, other apps will not
be notified of the change, and will in fact continue to get the
cookie from NSHTTPCookieStorage, for about 5 seconds after the cookie
is no longer available to your app. The same thing happens when your
app adds a cookie: Other apps will not be notified of the change, and
will not be able to get it from NSHTTPCookieStorage, until about 5
seconds after it is available to your app.
So, now I'm in the pickle of using an 8 second delay and worrying if
this will be long enough. Does anyone know:
BIG QUESTION: What is the basis of the roughly 5-second delay???
Thanks,
Jerry Krinock
If you want to run the test yourself, compile the code which follows
as a Foundation Tool project. Here's a typical console output:
2007-04-01 17:23:39.636 CookieCounter[10941] Notification: Google
cookie DIS-APPEARED
2007-04-01 17:24:02.637 CookieCounter[10941] Notification: Google
cookie APPEARED
2007-04-01 17:24:02.662 CookieCounter[10941] Polling: Google cookie
APPEARED
2007-04-01 17:24:23.731 CookieCounter[10941] Notification: Google
cookie DIS-APPEARED
2007-04-01 17:24:23.771 CookieCounter[10941] Polling: Google cookie
DIS-APPEARED
2007-04-01 17:24:42.623 CookieCounter[10941] Notification: Google
cookie APPEARED
2007-04-01 17:24:42.654 CookieCounter[10941] Polling: Google cookie
APPEARED
In the console output, "Polling" is the result of examining the array
returned by
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]
every 200 milliseconds, and "Notification" is the action caused by
name:NSHTTPCookieManagerCookiesChangedNotification
To log in and out of Google, visit
https://www.google.com/accounts/Login
https://www.google.com/accounts/Logout
Finally, note that this is a TOOL project. When I do this test on
a .app, I find that it does not get the changed cookies until the app
is relaunched. But that's another issue.
#import <Foundation/Foundation.h>
@interface CookieChecker : NSObject {
BOOL _googleLoggedIn ;
}
@end
@implementation CookieChecker
- (void)cookieChomp:(NSNotification*)notification {
NSHTTPCookieStorage* sharedCookieStorage =
[NSHTTPCookieStorage sharedHTTPCookieStorage] ;
NSArray* cookies = [sharedCookieStorage cookies] ;
NSEnumerator* e = [cookies objectEnumerator] ;
NSHTTPCookie* cookie ;
BOOL gotGoogle = NO ;
while ((cookie = [e nextObject])) {
NSString* domain = [cookie domain] ;
if ([domain hasSuffix:@"google.com"]) {
if ([[cookie name] isEqualToString:@"LSID"]) {
gotGoogle = YES ;
break ;
}
}
}
if (gotGoogle) {
NSLog(@"Notification: Google cookie APPEARED") ;
}
else {
NSLog(@"Notification: Google cookie DIS-APPEARED") ;
}
}
- (void)check:(NSTimer*)timer {
NSHTTPCookieStorage* sharedCookieStorage =
[NSHTTPCookieStorage sharedHTTPCookieStorage] ;
NSArray* cookies = [sharedCookieStorage cookies] ;
NSEnumerator* e = [cookies objectEnumerator] ;
NSHTTPCookie* cookie ;
BOOL gotGoogle = NO ;
while ((cookie = [e nextObject])) {
NSString* domain = [cookie domain] ;
if ([domain hasSuffix:@"google.com"]) {
if ([[cookie name] isEqualToString:@"LSID"]) {
gotGoogle = YES ;
break ;
}
}
}
if (gotGoogle != _googleLoggedIn) {
if (gotGoogle) {
NSLog(@"Polling: Google cookie APPEARED") ;
}
else {
NSLog(@"Polling: Google cookie DIS-APPEARED") ;
}
}
_googleLoggedIn = gotGoogle ;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Same code:
CookieChecker* checker = [[CookieChecker alloc] init] ;
[NSTimer scheduledTimerWithTimeInterval:0.20
target:checker
selector:@selector(check:)
userInfo:nil
repeats:YES] ;
[[NSNotificationCenter defaultCenter]
addObserver:checker
selector:@selector(cookieChomp:)
name:NSHTTPCookieManagerCookiesChangedNotification
object:nil] ;
[[NSRunLoop currentRunLoop] run] ;
[pool release];
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden