如何为临时NSURLSession设置cookieAcceptPolicy

我正尝试使用短暂的NSURLSessions为我的应用程序中的多个任务提供单独的Cookie处理。 这些任务不直接绑定到UI。 问题:无论我做什么,临时NSHTTPCookieStorage的cookieAcceptPolicy仍然是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的输出总是:

Ephemeral <NSHTTPCookieStorage cookies count:0>
1

其中1是NSHTTPCookieAcceptPolicyNever的值(NSHTTPCookieAcceptPolicyAlways预期为0)。 响应中的标题在那里。

当会话还活着时,我可以做些什么来让NSHTTPCookieStorage记住我的cookies? 我不需要也不想要任何持久性。 我只是想让cookies保留在内存中,以便在同一会话中用于进一步的请求。


它看起来像短暂的会议不存储有史以来的饼干。 爱斯基摩人在devforums上说道:

ISTR认为短暂的会话配置不能按照人们基于文档预期的方式工作。 我从来没有详细看过这个,但看起来像你。 你应该提交一个关于这个的错误; 实施和文件显然是不同步的,所以其中一个需要修复。


它看起来像iOS9 ephemeralSessionConfiguration按预期工作。 不过,在iOS8上,似乎Cookie存储器的策略返回“从不”,并且无法重置。 尽管存在专用类名称,但实现似乎是无存储的,而不是仅存储器。

对于iOS8,尽管我能够取代基本的实现,但它似乎可行(至少在模拟器中进行轻度测试)。 实现具有任务对象的新方法非常重要。

#import <Foundation/Foundation.h>
@interface MemoryCookieStorage : NSHTTPCookieStorage
@property (nonatomic, strong) NSMutableArray *internalCookies;
@property (atomic, assign) NSHTTPCookieAcceptPolicy policy;
@end

@implementation MemoryCookieStorage

- (id)init
{
    if (self = [super init]) {
        _internalCookies = [NSMutableArray new];
        _policy = NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain;
    }
    return self;
}

- (NSHTTPCookieAcceptPolicy)cookieAcceptPolicy {
    return self.policy;
}
- (void)setCookieAcceptPolicy:(NSHTTPCookieAcceptPolicy)cookieAcceptPolicy {
    self.policy = cookieAcceptPolicy;
}

- (NSUInteger)_indexOfCookie:(NSHTTPCookie *)target
{
    return [_internalCookies indexOfObjectPassingTest:^BOOL(NSHTTPCookie *cookie, NSUInteger idx, BOOL *stop) {
        return ([target.name caseInsensitiveCompare:cookie.name] == NSOrderedSame &&
                [target.domain caseInsensitiveCompare:cookie.domain] == NSOrderedSame &&
                (target.path == cookie.path || [target.path isEqual:cookie.path]));
    }];
}

- (void)setCookie:(NSHTTPCookie *)cookie
{
    if (self.cookieAcceptPolicy != NSHTTPCookieAcceptPolicyNever)
    {
        @synchronized(_internalCookies) {
            NSInteger idx = [self _indexOfCookie:cookie];
            if (idx == NSNotFound)
                [_internalCookies addObject:cookie];
            else
                [_internalCookies replaceObjectAtIndex:idx withObject:cookie];
        }
    }
}

- (void)deleteCookie:(NSHTTPCookie *)cookie
{
    @synchronized(_internalCookies) {
        NSInteger idx = [self _indexOfCookie:cookie];
        if (idx != NSNotFound)
            [_internalCookies removeObjectAtIndex:idx];
    }
}

- (NSArray *)cookies
{
    @synchronized(_internalCookies) {
        return [_internalCookies copy];
    }
}

static BOOL HasCaseSuffix(NSString *string, NSString *suffix)
{
    return [string rangeOfString:suffix options:NSCaseInsensitiveSearch|NSAnchoredSearch|NSBackwardsSearch].length > 0;
}
static BOOL IsDomainOK(NSString *cookieDomain, NSString *host)
{
    return ([cookieDomain caseInsensitiveCompare:host] == NSOrderedSame ||
            ([cookieDomain hasPrefix:@"."] && HasCaseSuffix(host, cookieDomain)) ||
            (cookieDomain && HasCaseSuffix(host, [@"." stringByAppendingString:cookieDomain])));
}
- (NSArray *)cookiesForURL:(NSURL *)URL
{
    NSMutableArray *array = [NSMutableArray new];
    NSString *host = URL.host;
    NSString *path = URL.path;

    @synchronized(_internalCookies)
    {
        for (NSHTTPCookie *cookie in _internalCookies)
        {
            if (!IsDomainOK(cookie.domain, host))
                continue;

            BOOL pathOK = cookie.path.length == 0 || [cookie.path isEqual:@"/"] || [path hasPrefix:cookie.path];
            if (!pathOK)
                continue;

            if (cookie.isSecure && [URL.scheme caseInsensitiveCompare:@"https"] != NSOrderedSame)
                continue;

            if ([cookie.expiresDate timeIntervalSinceNow] > 0)
                continue;

            [array addObject:cookie];
        }
    }

    array = (id)[array sortedArrayUsingComparator:^NSComparisonResult(NSHTTPCookie *c1, NSHTTPCookie *c2) {
        /* More specific cookies, i.e. matching the longest portion of the path, come first */
        NSInteger path1 = c1.path.length;
        NSInteger path2 = c2.path.length;
        if (path1 > path2)
            return NSOrderedAscending;
        if (path2 > path1)
            return NSOrderedDescending;
        return [c1.name caseInsensitiveCompare:c2.name];
    }];

    return array;
}

- (NSArray *)sortedCookiesUsingDescriptors:(NSArray *)sortOrder
{
    return [[self cookies] sortedArrayUsingDescriptors:sortOrder];
}

- (void)getCookiesForTask:(NSURLSessionTask *)task completionHandler:(void (^) (NSArray *taskCookies))completionHandler
{
    NSArray *urlCookies = [self cookiesForURL:task.currentRequest.URL];
    completionHandler(urlCookies);
}

- (void)setCookies:(NSArray *)newCookies forURL:(NSURL *)URL mainDocumentURL:(NSURL *)mainDocumentURL
{
    NSString *host = mainDocumentURL.host;
    for (NSHTTPCookie *cookie in newCookies)
    {
        switch (self.cookieAcceptPolicy)
        {
            case NSHTTPCookieAcceptPolicyAlways:
                [self setCookie:cookie];
                break;
            case NSHTTPCookieAcceptPolicyNever:
                break;
            case NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain:
                if (IsDomainOK(cookie.domain, host))
                    [self setCookie:cookie];
                break;
        }
    }
}

- (void)storeCookies:(NSArray *)taskCookies forTask:(NSURLSessionTask *)task
{
    NSURL *mainURL = task.currentRequest.mainDocumentURL ?: task.originalRequest.mainDocumentURL ?: task.originalRequest.URL;
    [self setCookies:taskCookies forURL:task.currentRequest.URL mainDocumentURL:mainURL];
}

@end

应该可以在创建临时会话之后测试sessionConfiguration.HTTPCookieStorage.cookieAcceptPolicy == NSHTTPCookieAcceptPolicyNever ,以查看HTTPCookieStorage是否需要用上述类的实例替换(iOS9上不需要它)。 有可能有一些错误...我只需要这个演示,它运作得很好。 但如果出现问题,他们不应该太难解决。

链接地址: http://www.djcxy.com/p/23771.html

上一篇: How to set cookieAcceptPolicy for ephemeral NSURLSession

下一篇: How can I make my Python script faster?