301 HTTP Redirects and iOS Caching

I just learned the hard way that iOS permanently caches HTTP 301 (“Moved Permanently”) redirect responses from web servers. This makes sense, given that 301s are reserved for permanent redirections. As a result, I ended up permanently blocking access to my application’s web-based components for anyone who had my app open at the time of the change. The problem was that I was merely testing something, and blocking one entire subdomain was a bug in my configuration.

The good news is that the caching appears to be on a per-app basis, since Safari could access my website but my apps could not. The even better news is that you can easily program your way around this caching. If you’re using the new NSURLSession classes (and you are, right?), you can simply set the requestCachePolicy of your NSURLSessionConfiguration variable to NSURLRequestReloadIgnoringLocalCacheData.

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];

Leave a Reply

Your email address will not be published. Required fields are marked *