iOS AFNetworking 2.0 how to make AFHTTPSessionManager to automatically apply cookies for a domain?

724 Views Asked by At

I have code that manually applies cookies for a domain to AFHTTPSessionManager's request serializer. When I execute the code, I see that cookies are being sent with requests. However, I cant seem to make the manager handle them automatically.

Is there a way to make AFHTTPSessionManager automatically apply cookies to requests for specific paths within a domain?

-(void)configureCookiesForManager:(AFHTTPSessionManager*)_manager
{

    NSArray *cookiesArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:_manager.baseURL];
    NSDictionary *cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookiesArray];

        for(NSString* key in cookieHeaders)
        {
            NSString* value = cookieHeaders[key];
            [_manager.requestSerializer setValue:cookieHeaders[key] forHTTPHeaderField:key];

}

I have tried:

cookieStorage.cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;
[NSURLSessionConfiguration defaultSessionConfiguration].HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways; //could be duplicate
[NSURLSessionConfiguration defaultSessionConfiguration].HTTPShouldSetCookies = YES;
__manager.requestSerializer.HTTPShouldHandleCookies = YES;

Here's what my cookie looks like:

[NSHTTPCookie]
  name            = name
  value           = MyPersistentCookie
  domain          = 10.1.1.111
  path            = /
  expiresDate     = 2017-02-19 19:08:57 +0000
  sessionOnly     = 0
  secure          = 0
  comment         = (null)
  commentURL      = (null)
  version 
0

There are 0 best solutions below