Using Cookies with Cocoa (NSHttpCookie)

I wrote a small post a few days ago about HTTP Post with Cocoa.

After that, I also needed to get the Cookies set on the server from my NSURLRequest… The documentation on NSHTTPCookie, to me anyway, seem unclear so, here is what I found to do it…

Get and save the cookie

After sending the request with NSURLConnection, we can easily get the response header fields from the response like this:

[NSURLConnection sendSynchronousRequest:request
                 returningResponse:&resp
                 error:&error];
/* Get all header fields */
[resp allHeaderFields];

Well, this method could be useful, but we still doesn’t have the potential cookie(s) after doing that. We need to extract the cookie(s) and we can store it thanks to NSHTTPCookieStorage:

/* Get an array with all the cookies */
NSArray* allCookies =
    [NSHTTPCookie cookiesWithResponseHeaderFields:[resp allHeaderFields]
                  forURL:[NSURL URLWithString:@"http://www.calaresu.eu/"]];
/* Add the array of cookies in the shared cookie storage instance */
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
    setCookies:allCookies
    forURL:[NSURL URLWithString:@"http://www.calaresu.eu/"]
    mainDocumentURL:nil];

The NSHTTPCookieStorage implements a shared instance that manages the shared cookie storage. The main advantage of this object, is that all cookies are shared among all applications and are kept in sync cross-process: see NSHTTPCookieStorage reference.
At this time, the allCookies array contain zero, one or more NSHTTPCookie, and we can get some properties from them like expiresDate, name, value, etc. For example:

for (NSHTTPCookie* cookie in availableCookies)
		NSLog(@"\nName: %@\nValue: %@\nExpires: %@", [cookie name], [cookie value], [cookie expiresDate]);

Use the saved cookie(s)

Right now, we have saved all the cookies from the server, we may need to use some informations from them (like the PHPSESSID) in order to access some web pages.

We can reuse the allCookies object, but more interesting… we can get all the cookies of our website from the NSHTTPCookieStorage instance and generate a new request with header fields using the cookies values. In order to get the shared cookie storage instance, we can call the Class Method [NSHTTPCookieStorage sharedHTTPCookieStorage]:

/* getting the stored cookies */
NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]
    cookiesForURL:[NSURL URLWithString:@"http://www.calaresu.eu"]];
/* Make a new header from the cookies */
NSDictionary* headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies]

The next steps, like usually, make a request with the headers we just made. For example:

NSMutableURLRequest* req = [[NSMutableURLRequest alloc] init];
	[req setURL:[NSURL URLWithString:@"http://www.calaresu.eu/cookie.php"]];
	[req setAllHTTPHeaderFields:headers];
/* Send the request */
[NSURLConnection sendSynchronousRequest:req
                 returningResponse:&response
                 error:&error];

Right now, we can send request with some cookies… to keep a session, to keep some preferences, etc.


 




Security Code: