iPhone SDK: Cache Policy & Cookie Handling in NSURLRequest
I recently ran into an issue building an app which hit an existing (protected) website. Passing credentials along with each request, responses at first appeared to be cached, even after the user credentials changed from within the iPhone app. In sorting through this issue, it gave me a chance to get a bit more familiar with NSURLRequest, and NSURLRequestCachePolicy. As I later found, however, my problem turned out to be cookie-related..
Starting out, I assumed there to be a relationship between setting the cache policy on NSURLRequest and the behavior I would see when implementing connection:willCacheResponse: from within my delegate. When setting the cache policy to NSURLRequestReloadIgnoringLocalCacheData did not solve my problem, I implemented the latter delegate method in order to intercept any cached response.
I spent an hour or so of looking into this issue, with no success. Grabbing a late cup of coffee, I rolled back my changes and took a fresh look at things.
On the one hand, I was convinced by this point that the responses were not actually being cached. In fact, I felt had the feeling that the problem had nothing to do with cached responses at all. After double checking to make sure I was not holding on to the original credentials elsewhere in my code (stuffed inside an instance variable perhaps), it finally dawned on me that the problem might be related to the site I was connecting to, and its use of cookies to keep track of the user's identity after a successful login.
Fast forward ten minutes and one cup of coffee to the realization that this was indeed the case. Upon the first successful login, the site sets a cookie which NSURLRequest passes along on subsequent requests, even when the underlying credentials had changed! The fix here (in my adapter code) seemed pretty straightforward:
- When the user credentials change, ignore any cached responses
- When the user credentials change, invalidate any cookies
I changed my HTTP adapter class to expose the underlying 'cachePolicy', and to then contain the following code when constructing new instances of NSURLRequest:
NSMutableURLRequest *req;
req = [NSMutableURLRequest requestWithURL:url
cachePolicy:cachePolicy
timeoutInterval:60.0];
if (cachePolicy == NSURLRequestReloadIgnoringCacheData) {
[req setHTTPShouldHandleCookies:NO];
}
This solution is a bit of a stopgap-- the only time I ever need to ignore the cache is when the user credentials change while the application is running. Every other caller of this class expects the default cache policy. And this "fix" has more to do with the behavior of the existing website I access (and its use of cookies) than NSURLRequest per se.
In short, there is good news and bad news. The good news is that it works. The bad news is that I am still not completely happy about the solution. The only silver lining here is that the implementation did not involve too much hand waving, and should be easy to remove once I find a better way.
While it is good to know that one can disable cookie support on a request-by-request basis, I will probably take the next step and model the user credentials in a way that makes this type of behavior transparent to all other kinds of HTTP requests.
Topics: iPhone, networking, web
Comments: 2 so far
Leave a comment
About Pathfinder
Follow the Blog
-
Get a monthly update on best practices for delivering successful software.
Subscribe via email
Subscribe via RSS
Categories
Topics
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
Blogroll
Recent
- Elements of Testing Style
- Aesthetics and Web Design
- Asterisk-Java Testing with Groovy
- 3 Misuses of Code Comments
- Fluently NHibernate
- Digging a Hole and Covering it with Leaves — The Software Development Version
- The Importance of User Experience - Do You Understand It in Your Bones?
- Writing Your Own Protocol With NSURLProtocol
- What’s In Your Dock: iPhone edition
- Feature Fatigue

Thank you! This helped me figure out something that was driving me insane!
Comment by Workshed, Sunday, May 3, 2009 @ 6:16 am
If I’d sell my application, I’d give 20% of my earnings to you.. Unfortunately, it’ll be free
Thanks a lot man. Please keep writing
Comment by Jim, Tuesday, May 5, 2009 @ 5:48 pm