0

I'm creating a cookie using NSHTTPCookie. But when I create the expiration date is getting converted to creation date. Here is my code:

NSMutableDictionary *cProperties = [NSMutableDictionary dictionary];

   [cProperties setObject:@"31 May 2016 17:04:14 GMT" forKey:NSHTTPCookieExpires];
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

Per console output:

    <NSHTTPCookie version:0 name:"myCookie" 
 expiresDate:(null) created:2016-05-31 16:32:37 +0000 
sessionOnly:TRUE  path:"/" isSecure:TRUE>

Any of you knows why my expiresDate is getting switch with created date ?

I'll really appreciate your help.

user2924482
  • 8,380
  • 23
  • 89
  • 173

2 Answers2

1

You aren't passing the properties to the cookie. You want:

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cProperties];
                                                          ^^^^^^^^^^^

Also it's probably easier to set the expires date/time using an NSDate object instead of an NSString.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

NSHTTPCookieExpires is expecting NSDate as a value type, based on Apple's document So you have to use NSDate type value such as [NSDate dateWithTimeIntervalSinceNow:60*60] to set NSHTTPCookieExpires key

riowww
  • 123
  • 1
  • 10
  • The [documentation](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/#//apple_ref/doc/constant_group/HTTP_Cookie_Attribute_Keys) says it accepts either `NSDate` or `NSString` when set via the properties. I agree that using `NSDate` is better though. – trojanfoe May 31 '16 at 21:33
  • @trojanfoe yes, you are right. NSString is acceptable. I think the problem is the format of the NSString, it needs to be something like "2016-06-01 23:50:39 +0000", follow the NSDate or look at the value for "created" – riowww Jun 01 '16 at 22:53