xiabin
xiabin

Reputation: 101

How to disable HTTP cache in iOS

As we all know that the standard HTTP protocol use 'Cache-Control' to manage cache. If we set 'max-age = negative number' for the key, NSURLConnection will not connect to network, but return the cache immediately. Recently, I run into a rediculous requirement. I was told to disable HTTP cache, event if the response header indicate that cache exists. And I can not set 'Cache-Control' for request header. Just like a browser. I know the iOS implement the standard HTTP protocol, I can't change the behavior of NSURLConnection. Any one can help? Thanks a lot.

Upvotes: 1

Views: 1467

Answers (1)

Caleb
Caleb

Reputation: 124997

The documentation for NSURLSessionConfiguration says of that class's URLCache property:

To disable caching, set this property to nil.

Therefore, you should create your NSURLSession using a configuration with URLCache set to nil:

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.URLCache = nil;
NSURLSession *session = [NSURLSession sessionWithConfiguration: defaultConfigObject 
                                                      delegate:self 
                                                 delegateQueue:[NSOperationQueue mainQueue]];

Upvotes: 1

Related Questions