NSHTTPCookieStorage and Cookie Expiration Date

In our iPhone application, we use two cookies during communication with the server. One is a short session cookie (JSESSION), and the other is a long session cookie (REMEMBER ME). If the response comes from the server, it sends a short session cookie, which I can find in NSHTTPCookieStorage.

My question is, how does this store handle cookie expiration dates? So if the cookie expires, it automatically deletes this cookie, and if I try to get this cookie by its name from the store after the expiration date, will I get something? Or do I need to check the expiration manually?

+4
source share
2 answers

My question is, how does this store handle cookie expiration dates?

NSHTTPCookieStorage stores NSHTTPCookie objects whose expiration date is one of its properties.

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/occ/cl/NSHTTPCookie

So, if the cookie expires, it automatically deletes this cookie, and if I try to get this cookie by its name from the store after the expiration date, will I get something? Or do I need to check the expiration manually?

You must manually check for expiration and delete the cookie yourself

As stated in http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/occ/cl/NSHTTPCookie

The receiver's expiration date, or nil if there is no specific expiration date such as in the case of "session-only" cookies. The expiration date is the date when the cookie should be deleted. 
+5
source

To be more practical ...

 +(BOOL) isCookieExpired{ BOOL status = YES; NSArray *oldCookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ] cookiesForURL: [NSURL URLWithString:kBASEURL]]; NSHTTPCookie *cookie = [oldCookies lastObject]; if (cookie) { NSDate *expiresDate = [cookie expiresDate]; NSDate *currentDate = [NSDate date]; NSComparisonResult result = [currentDate compare:expiresDate]; if(result==NSOrderedAscending){ status = NO; NSLog(@"expiresDate is in the future"); } else if(result==NSOrderedDescending){ NSLog(@"expiresDate is in the past"); } else{ status = NO; NSLog(@"Both dates are the same"); } } return status; } 
+4
source

Source: https://habr.com/ru/post/1369137/


All Articles