Question

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];
Was it helpful?

Solution 2

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];
    }
}

OTHER TIPS

Using NSHTTPCookieStorage, you can interrogate each cookie before you decide to delete it (using domain / name). In this way you can filter the deletions to only the domain you're interested in.

Using RestKit, you can use the RKObjectManager to create the NSURLRequest using requestWithObject:method:path:parameters: (or one of the related methods), and then you can modify the request and action it yourself (possibly using RestKit operations).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top