如何设置cookieAcceptPolicy临时NSURLSession [英] How to set cookieAcceptPolicy for ephemeral NSURLSession

查看:559
本文介绍了如何设置cookieAcceptPolicy临时NSURLSession的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用临时NSURLSessions来为我的应用程序中的多个任务提供单独的Cookie处理。这些任务不直接绑定到UI。
问题:无论我做什么,临时NSHTTPCookieStorage的cookieAcceptPolicy保持 NSHTTPCookieAcceptPolicyNever

I am trying to use ephemeral NSURLSessions to provide separate cookie handling for several tasks within my app. These tasks are not directly bound to a UI. Problem: Whatever I do, the cookieAcceptPolicy of the ephemeral NSHTTPCookieStorage remains NSHTTPCookieAcceptPolicyNever.

这是我的代码:

// use a pure in-memory configuration with its own private cache, cookie, and credential store
__block NSURLSessionConfiguration* config = [NSURLSessionConfiguration ephemeralSessionConfiguration];

// do anything to accept all cookies
config.HTTPShouldSetCookies = YES;
config.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;
config.HTTPCookieStorage.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;

__block NSURLSession* session = [NSURLSession sessionWithConfiguration:config];

NSURLSessionDataTask* task = [session dataTaskWithURL:[NSURL URLWithString:@"https://test.cgmlife.com/Catalogs"]
                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                        NSHTTPCookieStorage* cookies = session.configuration.HTTPCookieStorage;
                                        NSLog(@"%@", cookies);
                                        NSLog(@"%lu", cookies.cookieAcceptPolicy);
                                    }];
[task resume];

NSLog的输出总是:

The output from NSLog is always:

Ephemeral <NSHTTPCookieStorage cookies count:0>
1

其中1是NSHTTPCookieAcceptPolicyNever的值(NSHTTPCookieAcceptPolicyAlways的预期值为0)。

where 1 is the value for NSHTTPCookieAcceptPolicyNever (expected 0 for NSHTTPCookieAcceptPolicyAlways). The headers in the response are there.

我可以做什么让NSHTTPCookieStorage记住我的Cookie,而会话仍然存在?我不需要,也不想要任何持久性。

What can I do to have the NSHTTPCookieStorage remember my cookies while the session is alive? I don't need and don't want any persistence. I just want to keep cookies in memory so that they are reused for further requests in the same session.

推荐答案

它看起来像短暂的会话不存储Cookie。 eskimo1在devforums 上说:

It looks like ephemeral sessions don't store cookies ever. eskimo1 says on the devforums:

ISTR,临时会话配置不按照
所期望的基于文档的方式工作。我从来没有到过
看这个细节,但似乎你有。你应该提交
一个关于这个的错误;实施和文档清楚地显示出
的同步,因此其中一个需要修复。

ISTR that ephemeral session configurations don't work the way that folks expect based on the documentation. I never got around to looking at this in detail but it seems like you have. You should file a bug about this; the implementation and documentation are clearly out of sync, so one of them needs to be fixed.

这篇关于如何设置cookieAcceptPolicy临时NSURLSession的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆