Reputation: 4042
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
.
[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
.NSDate
using NSDateFormatter
, the output is NSString
.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
?
Upvotes: 4
Views: 1452
Reputation: 1008
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
.
Upvotes: 1