4
votes

I want to set a NSHTTPCookie in the NSHTTPCookieStorage.

For doing that, I am creating a cookie & adding it to the NSHTTPCookieStorage as shown below:-

NSDictionary  *propertiesDevice = [NSDictionary dictionaryWithObjectsAndKeys:
                                   @"http://www.sample.com/", NSHTTPCookieDomain,
                                   @"/", NSHTTPCookiePath,
                                   @"someCookieName", NSHTTPCookieName,
                                   @"myCookieValue", NSHTTPCookieValue,
                                   timeStamp,NSHTTPCookieExpires,nil];
NSHTTPCookie *cookieDevice = [NSHTTPCookie cookieWithProperties:propertiesDevice];
[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookie:cookieDevice];

All the properties of the NSHTTPCookie sets as expected, apart from the NSHTTPCookieExpires.

  1. If I pass [NSDate date], then the value is set. But the problem is it is not in the desired NSHTTPCookie format of Expires=Tue, 15-Jan-2013 21:47:38 GMT. The format of [NSDate date] is 2014-06-19 12:04:00 +0000.
  2. When I try to change the format of NSDate using NSDateFormatter, the output is NSString.
  3. The problem is that no matter what NSString I set to NSHTTPCookieExpires key, it takes null value.

Though in the documentation it says that NSHTTPCookieExpires takes NSString or NSDate.

    From Apple Doc:-

    <td>NSHTTPCookieExpires</td>
    <td>NSDate or NSString</td>
    <td>NO</td>
    <td>Expiration date for the cookie. Used only for version 0
    cookies. Ignored for version 1 or greater.</td>

Does anyone have any idea as to why we are not able to set NSString value for the key NSHTTPCookieExpires?

1

1 Answers

1
votes

In my case it wasn't working because I was cloning an existing cookie and NSHTTPCookieDiscard was defined there.

Removing it solved the problem.

There are other rules that can make the expiration date nil, even if you set it with a valid NSDate or NSString.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/