Search code examples
objective-cmacoscookiesrestkitnsurlrequest

Restkit equivalent of `[NSMutableURLRequest HTTPShouldHandleCookies]`


I have two pieces of software with a different approach:

  1. using NSMutableURLRequest
  2. using RestKit

In 1. I have the following line:

request.HTTPShouldHandleCookies = NO;

Since "approach 1." works fine I am now looking for an equivalent for the approach using RestKit. I get the same behaviour when I delete all the cookies in the [NSHTTPCookieStorage sharedHTTPCookieStorage] like so:

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookieStorage.cookies)
{
    [cookieStorage deleteCookie:cookie];
}

However, this deletes all the cookies system-wide so I am signed out of the affected websites in my browser.

I am now looking for a way to get an equivalent of HTTPShouldHandleCookies with RestKit.

Update:

I am using RestKit like so:

[manager putObject: myObject path: path parameters:nil success:nil];

Solution

  • I now found a work-around to solve the problem**:

    1. Delete the cookie from the store (save the cookie for later)
    2. Issue the request using RestKit as normal
    3. Re-add the cookie to the cookie store in any completion blocks

    ** However, any clean solutions are very welcome and will happily be marked as answer

    These are the methods I used:

    - (void) disableSessionCookie{
        NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSArray *cookiesToDelete = @[@"myspecial_session"];
    
        for (NSHTTPCookie *cookie in cookieStorage.cookies){
            if([cookie.domain rangeOfString:@"myspecialdomain"].location != NSNotFound){
                if([cookiesToDelete containsObject:cookie.name]){
                    self.sessionCookie = cookie;
                    [cookieStorage deleteCookie:cookie];
                }
            }
        }
    }
    
    - (void) enableSessionCookie{
    
        if(self.sessionCookie){
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:self.sessionCookie];
        }
    }