创建具有到期日期的 Cookie

罗特姆·雅科夫

我想用 来创建一个 cookie sessionOnly = False,我明白这样做是有必要设置一个到期日期的。但是,未设置到期日期属性。

// set properties dictionary
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];

// insert all values
[cookieProperties setObject:@"myDomain" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"myName" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"myValue" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"FALSE" forKey:NSHTTPCookieDiscard];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];

// setting expiry date for a month from now
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];

// create a cookie with these properties
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];

当我查看cookieProperties价值时,我得到:

{
    Discard = FALSE;
    Domain = myDomain;
    Expires = "2019-10-08 23:43:44 +0000";
    Name = myName;
    Path = "/";
    Value = myValue;
    Version = 0;
}

正如它应该。

cookie价值是:

<NSHTTPCookie
    version:0
    name:myName
    value:myValue
    expiresDate:'(null)'
    created:'2019-09-08 13:14:46 +0000'
    sessionOnly:TRUE
    domain:mydomain
    partition:none
    sameSite:none
    path:myPath
    isSecure:FALSE
 path:"myPath" isSecure:FALSE>

随着expiresDate:'(null)'sessionOnly:TRUE这是为什么?

罗特姆·雅科夫

原来是删除线

[cookieProperties setObject:@"FALSE" forKey:NSHTTPCookieDiscard];

解决了。

这个答案对我有帮助,它说要删除这个属性,我认为将它设置为 false 会“更强”。

再次遇到它后,我尝试实际将其删除,并且做到了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章