iOS Cookie Maintenance

Based on using Android volley to connect to Windows IIS, for iOS, it would be easier to maintain a cookie from server side.

If you are using this library – AFNetworking, basically, you don’t need to worry about cookie handling.

 Whether created requests should use the default cookie handling. `YES` by default.

 @see NSMutableURLRequest -setHTTPShouldHandleCookies:
@property (nonatomic, assign) BOOL HTTPShouldHandleCookies;

According to document here, default setting value is YES! Unless you create your own RequestSerializer, or you don’t need to setup that as YES. If you did that, you need to setup default value.

In your AFNetworking callback function, you will see:

NSDictionary *fields = ((NSHTTPURLResponse*)task.response).allHeaderFields;
NSLog(@"fields = %@",[fields description]);

NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *cookie in cookies) {
    NSLog(@"Block cookie: %@", cookie);

We get allHeaderFields from network response which has all headers. NSHTTPCookieStorage is a global use class for handling cookies for all network connection on your phone. We can check if what cookies it stored.

Basically, if you setup everything correctly, you will see logs in console:

fields = {
    "Cache-Control" = "no-cache";
    "Content-Length" = 34;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Mon, 13 Feb 2017 16:34:28 GMT";
    Expires = "-1";
    Pragma = "no-cache";
    Server = "Microsoft-IIS/8.5";
    "Set-Cookie" = "ASP.NET_SessionId=thrsd3sgnif14ohclkybueo3; path=/; HttpOnly";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
<NSHTTPCookie version:0 name:"ASP.NET_SessionId" value:"thrsd3sgnif14ohclkybueo3" expiresDate:(null) created:2017-02-13 16:34:30 +0000 sessionOnly:TRUE domain:"------------" partition:"none" path:"/" isSecure:FALSE>

It shows system has been saving that for you, and you don’t need to handle that again.

For those cookies, you might wanna store that and use in the header.

NSArray *cookiesArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
NSDictionary *cookieDict = [NSHTTPCookie requestHeaderFieldsWithCookies:cookiesArray];
NSString *cookie = [cookieDict objectForKey:@"Cookie"];
[NSURLRequest setValue:cookie forHTTPHeaderField:@"Cookie"];

After using that, you can delete that cookie if you don’t wanna use anymore.

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

That’s it.

Happy coding.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s