问题
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
.
- If I pass
[NSDate date]
, then the value is set. But the problem is it is not in the desiredNSHTTPCookie
format ofExpires=Tue, 15-Jan-2013 21:47:38 GMT
. The format of[NSDate date]
is2014-06-19 12:04:00 +0000
. - When I try to change the format of
NSDate
usingNSDateFormatter
, the output isNSString
. - The problem is that no matter what
NSString
I set toNSHTTPCookieExpires
key, it takesnull
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:
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/
来源:https://stackoverflow.com/questions/24306275/set-nshttpcookieexpires-manually