11
votes

Can someone please show me how to save cookies FOREVER using AFNetworking for iOS? I'm using ios7 but I think this should not matter?

The use case is that upon successful authentication with a webservice, it gives me two cookies that I need to pass along with every single request. These cookies never expire, or expire years from now. As long as I have the cookies with each HTTPS request, the user doesn't have to login again. I understand that AFNetworking automatically persists cookies until you quit the app, but I need the cookies to last forever until the user deletes the app from their phone.

1) Upon successful authentication, the webservice hands me two cookies. How do I access them? Do I go straight to NSHTTPCookie storage and grab the cookie by its name, or is there an AFNetworking "way" to do this?

2) How do I save these two magic cookies permanently so that my subclass of AFHTTPClient passes these two magic cookies along with each request? Do I just save them inside a keychain or NSUserDefaults or NSURLCredentialsStorage? Or again is there an AFNetworking way to do this? I read about a setAuthorization() method inside AFNetworking but I'm not sure if this applies to username/password and cookies as well.

3) How do I delete these cookies the AFNetworking way?

Thanks!!

1

1 Answers

20
votes

AFNetworking doesn't do anything explicitly with cookies so there is no "AFNetworking way" of dealing with cookies. You'll work with NSHTTPCookie and NSHTTPCookieStorage.

In my projects I didn't need to do anything specific to the requests to get them to work with cookies. If you need different request behavior than AFNetworking provides by default, you override requestWithMethod:path:parameters: in your AFHTTPClient subclass.

  1. To get the cookies returned from an AFJSONRequest, for example

    NSURLRequest* request = <some request returned from your AFHTTPClient subclass>
    AFJSONRequestOperation* op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
     success:^(NSURLRequest* request, NSHTTPURLResponse* response, id userData) {
        NSArray* cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:url];
     }
     failure:...
    
  2. This answer says to set an expiration date on the cookie. If you want to persist the cookies yourself, you can convert the cookies to a dictionary and save that. This answer says to use the keychain.

    NSMutableDictionary* cookieDictionary = [NSMutableDictionary dictionaryWithDictionary:cookiePrefs];
    for (NSHTTPCookie* cookie in cookies) {
       [cookieDictionary setValue:cookie.properties forKey:cookie.name];
    }
    
  3. Remove the cookies from the NSHTTPCookieStorage. There is some trickiness to watch out for. If you've saved the cookies yourself, delete the stored values however is appropriate.

    NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:theUrl];
    for (NSHTTPCookie* cookie in cookies) {
       [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }