NSHTTPCookieStorage 和 Cookie 过期日期

发布于 2024-12-01 16:31:45 字数 280 浏览 2 评论 0原文

在我们的 iPhone 应用程序中,我们在服务器通信期间使用两个 cookie。一种是短会话 cookie (JSESSION),另一种是长会话 cookie (REMEMBER ME)。如果答案来自服务器,它会发送一个短会话 cookie,我可以在 NSHTTPCookieStorage 中找到它。

我的问题是这个存储如何处理 cookie 的过期日期?因此,如果 cookie 过期,它是否会自动删除该 cookie,并且如果我在过期后尝试通过其名称从存储中获取此 cookie,我会得到什么吗?还是我必须手动检查过期时间?

In our iPhone app we use two cookies during server communication. One is a short session cookie (JSESSION), and the other is a long session cookie (REMEMBER ME). If an answer comes from the server, it sends a short session cookie, which I can find in the NSHTTPCookieStorage.

My question is how this storage handles the cookie's expiration date? So if the cookie expires, does it delete that cookie automatically, and if I try to get this cookie by its name from the storage after expiration, do I get anything? Or do I have to check the expiration manually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2024-12-08 16:31:45

我的问题是这个存储如何处理 cookie 的过期日期?

NSHTTPCookieStorage 存储将过期日期作为其属性之一的 NSHTTPCookie 对象。

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/occ/cl/NSHTTPCookie

因此,如果 cookie 过期,它是否会自动删除该 cookie,并且如果我尝试在过期后通过存储中的名称获取此 cookie,我会得到什么吗?还是我必须手动检查过期时间?

您应该手动检查过期情况并自行删除 cookie,

正如 http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/occ/cl/NSHTTPCookie

The receiver’s expiration date, or nil if there is no specific expiration date such as in the case of “session-only” cookies. The expiration date is the date when the cookie should be deleted.

My question is how this storage handles the cookie's expiration date?

NSHTTPCookieStorage stores the NSHTTPCookie objects that has expiration date as one of its property.

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/occ/cl/NSHTTPCookie

So if the cookie expires, does it delete that cookie automatically, and if I try to get this cookie by it's name from the storage after expiration, do I get anything? Or do I have to check the expiration manually?

You should check manually for expiration and delete the cookie yourself

As it is referred in http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSHTTPCookie_Class/Reference/Reference.html#//apple_ref/occ/cl/NSHTTPCookie

The receiver’s expiration date, or nil if there is no specific expiration date such as in the case of “session-only” cookies. The expiration date is the date when the cookie should be deleted.
泪眸﹌ 2024-12-08 16:31:45

为了更加实用...

+(BOOL) isCookieExpired{

    BOOL status = YES;

    NSArray *oldCookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
                           cookiesForURL: [NSURL URLWithString:kBASEURL]];
    NSHTTPCookie *cookie = [oldCookies lastObject];
    if (cookie) {
        NSDate *expiresDate =    [cookie expiresDate];
        NSDate *currentDate = [NSDate date];
        NSComparisonResult result = [currentDate compare:expiresDate];

        if(result==NSOrderedAscending){
            status = NO;
            NSLog(@"expiresDate is in the future");
        }
        else if(result==NSOrderedDescending){
            NSLog(@"expiresDate is in the past");
        }
        else{
            status = NO;
            NSLog(@"Both dates are the same");
        }
    }

    return status;
}

To be more practical...

+(BOOL) isCookieExpired{

    BOOL status = YES;

    NSArray *oldCookies = [[ NSHTTPCookieStorage sharedHTTPCookieStorage ]
                           cookiesForURL: [NSURL URLWithString:kBASEURL]];
    NSHTTPCookie *cookie = [oldCookies lastObject];
    if (cookie) {
        NSDate *expiresDate =    [cookie expiresDate];
        NSDate *currentDate = [NSDate date];
        NSComparisonResult result = [currentDate compare:expiresDate];

        if(result==NSOrderedAscending){
            status = NO;
            NSLog(@"expiresDate is in the future");
        }
        else if(result==NSOrderedDescending){
            NSLog(@"expiresDate is in the past");
        }
        else{
            status = NO;
            NSLog(@"Both dates are the same");
        }
    }

    return status;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文