Long story short, I just updated to Xcode 6 to check how my app works on iOS 8. I noticed that it doesn\'t use the cache even though it should. I\'m using AFNetworking setting t
I'm almost certain that iOS 8.0 has broken NSURLSession
's ability to cache HTTP response data. I've opened a radar with Apple about this issue.
Here's some sample code I wrote to prove this:
NSURLSession *session = [NSURLSession sharedSession];
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:32 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURL *URL = [NSURL URLWithString:@"http://i.imgur.com/b5pyONe.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:5];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error occurred");
} else {
NSLog(@"Fetched resource!");
}
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
request = [NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestReturnCacheDataDontLoad
timeoutInterval:5];
task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Something bad happened: %@", error);
} else {
NSLog(@"Fetched resource!");
}
}];
[task resume];
Even creating your own NSURLSession
-- with an NSURLSessionConfiguration
that has an NSURLCache
that you create yourself -- won't fix this issue. For now, if you need cached responses badly, you have to use NSURLConnection
.