Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLCache not storing cached data in iOS 7

Below given code works in iOS 6 but it does not work on iOS 7.

NSCachedURLResponse cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
[[NSURLCache sharedURLCache] storeCachedResponse:cachedURLResponse forRequest:request];
NSLog(@"Cached response is %@", [[NSURLCache sharedURLCache] cachedResponseForRequest:request]);

In iOS 6 I am getting the cached response data but in iOS 7 it returns null. I have set the NSURLCache object in App delegate using:

NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                     diskCapacity:2 * 1024 * 1024
                                                         diskPath:nil];
[NSURLCache setSharedURLCache:urlCache];

What is the reason of caching not working in iOS 7?

like image 294
Ratan Avatar asked Nov 11 '22 19:11

Ratan


1 Answers

Caching mechanism relies on HTTP headers. Check returned HTTP headers if caching is allowed in first place. You may do it with web proxies like 'Charles'.

like image 121
Juraj Antas Avatar answered Nov 15 '22 06:11

Juraj Antas