Fighting this problem and inability to implement a custom cookie management system.
It seems that some hidden iOS HTTP implementation level does not allow cookies to be processed correctly without sessions. Each time an HTTP response sets or removes a cookie, immediately checking the NSHTTPCookieStorage cookies gives the expected results and indicates the correct sessionOnly value.
But if the application closes shortly after the cookie updates response, after restarting these cookies, sessionOnly = FALSE will be returned to the previous state and the last lost updates.
Cookies set / deleted by response header or NSHTTPCookieStorage setCookie: does not matter.
Some voodoo caching / synchronization should happen behind the scenes. It takes up to 5 seconds for the cookie to become permanent.
Anyone who has or can point to any specific explanation for this behavior? Is this a mistake, simple and simple? Or some kind of undocumented function whose purpose I cannot understand?
Some code you can use to play:
- (void)applicationDidBecomeActive:(UIApplication *)application { [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; NSHTTPCookie *cookie; for (cookie in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) { NSLog(@"%@=%@", cookie.name, cookie.value); } NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary]; [cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName]; [cookieProperties setObject:[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]] forKey:NSHTTPCookieValue]; [cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain]; [cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL]; [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath]; [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
This code should output a new value every time it starts. Instead, you will see that if you exit the application quickly, the value will not change.
Some possible stack overflow problems:
iphone NSHTTPCookieStorage available to reopen application
iPhone: NSHTTPCookie not saved through application reloads
NSHTTPCookies refuse to be deleted
remote NSHTTPCookie returns if application terminated