手动设置NSHTTPCookieExpires

时间:2014-06-19 12:09:31

标签: ios objective-c nshttpcookie

我想在NSHTTPCookie中设置NSHTTPCookieStorage

为此,我正在创建一个cookie&将其添加到NSHTTPCookieStorage,如下所示: -

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];

除了NSHTTPCookie之外,NSHTTPCookieExpires的所有属性都按预期设置。

  1. 如果我通过[NSDate date],则设置该值。但问题是它不是NSHTTPCookie格式的Expires=Tue, 15-Jan-2013 21:47:38 GMT[NSDate date]的格式为2014-06-19 12:04:00 +0000
  2. 当我尝试使用NSDate更改NSDateFormatter的格式时,输出为NSString
  3. 问题在于,无论NSString设置为NSHTTPCookieExpires密钥,都需要null值。
  4. 虽然在文档中它说明NSHTTPCookieExpires需要NSStringNSDate

        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>
    

    有没有人知道为什么我们无法为密钥NSString设置NSHTTPCookieExpires值?

1 个答案:

答案 0 :(得分:0)

就我而言,它不起作用,因为我正在克隆现有的cookie并在那里定义了NSHTTPCookieDiscard

删除它解决了这个问题。

即使您使用有效的nilNSDate设置过期日期,还有其他规则可以设置过期日期NSString

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