Van Du Tran Van Du Tran - 1 year ago 133
iOS Question

How to disable caching from NSURLSessionTask

In my iOS app, I am using

to download json data to my app. I discovered that when I call the url directly from the browser, I get an up to date json and when it's called from within the app, I get an older version of the json.

Is this due to caching? How can I tell
to not use caching.

This is the call I use:

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {


Rob Rob
Answer Source

Rather than using the sharedSession, you also can create your own NSURLSession using a NSURLSessionConfiguration that specifies a default cache policy. So, define a property for your session:

@property (nonatomic, strong) NSURLSession *session;

And then:

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

Then requests using that session will use that requestCachePolicy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download