17

Here is the sample function I call when i need to clear cache and make a new call to URL

- (void)clearDataFromNSURLCache:(NSString *)urlString
{
    NSURL *requestUrl = [NSURL URLWithString:urlString];
    NSURLRequest *dataUrlRequest = [NSURLRequest requestWithURL: requestUrl];

    NSURLCache * cache =[NSURLCache sharedURLCache];


    NSCachedURLResponse* cacheResponse =[cache cachedResponseForRequest:dataUrlRequest];

    if (cacheResponse) {
        NSString* dataStr = [NSString stringWithUTF8String:[[cacheResponse data] bytes]];
        NSLog(@"data str r= %@",dataStr);
        NSLog(@"url  str r= %@",[[[cacheResponse response] URL] absoluteString]);
        [cache storeCachedResponse:nil forRequest:dataUrlRequest];
        [NSURLCache setSharedURLCache:cache];
    }

    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:dataUrlRequest];

    //Check if the response data has been removed/deleted from cache
    NSURLRequest *finalRequestUrlRequest = [NSURLRequest requestWithURL:requestUrl];
    NSURLCache * finalCache =[NSURLCache sharedURLCache];

    NSCachedURLResponse* finalcacheResponse =[finalCache cachedResponseForRequest:finalRequestUrlRequest];

    if (finalcacheResponse) {
        //Should not enter here
        NSString* finaldataStr = [NSString stringWithUTF8String:[[finalcacheResponse data] bytes]];
        NSLog(@"data str r= %@",finaldataStr);
        NSLog(@"url  str r= %@",[[[cacheResponse response] URL] absoluteString]);
    }
}

In iOS 6/7 the response is deleted successfully for the requestURL, but in iOS 8 it never gets deleted. I have searched but could not find any reason why this should not work in iOS8.

Any help will be appreciated…..

Airsource Ltd
  • 32,379
  • 13
  • 71
  • 75
nkp
  • 181
  • 2
  • 12

3 Answers3

21

NSURLCache is broken on iOS 8.0.x - it never purges the cache at all, so it grows without limit. See http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/ for a detailed investigation. Cache purging is fixed in the 8.1 betas - but removeCachedResponseForRequest: is not.

removeCachedResponsesSinceDate: does appear to work on iOS 8.0 - an API that was added for 8.0, but hasn't made it to the docs yet (it is in the API diffs). I am unclear what use it is to anyone - surely what you normally want to do is remove cached responses before a particular date.

removeAllCachedResponses works as well - but that's a real sledgehammer solution.

Marco
  • 6,692
  • 2
  • 27
  • 38
Airsource Ltd
  • 32,379
  • 13
  • 71
  • 75
  • 1
    I am also seeing removeCachedResponseForRequest: not working in iOS 8.3. – osxdirk May 05 '15 at 16:52
  • 4
    Broken in 9.3 & 9.3.1 – mmackh Apr 12 '16 at 06:26
  • 1
    Still broken in 11.2 :( Unfortunately this is critical to my project's 'prefetch' capability that relies on manipulating the cache entries. Oh well... – Chang Kuang Mar 09 '18 at 21:11
  • on iOS11: I'm attempting to use [`storeCachedResponse`](https://developer.apple.com/documentation/foundation/urlcache/1414434-storecachedresponse) for my question here: [URLresponse is not retrieved after storing in cache using storeCachedResponse](https://stackoverflow.com/questions/52938033/urlresponse-is-not-retrieved-after-storing-in-cache-using-storecachedresponse). But the API seems to not work. My guess is that **both** `store` and `remove` are broken. Any thoughts? – mfaani Oct 30 '18 at 16:50
  • 2
    Still broken in iOS 12.2! – Kawe May 08 '19 at 20:13
  • Is it still broken on latest iOS versions? – schinj Jan 22 '20 at 09:20
  • @schin Don't know. Haven't needed to look. You could use the source provided on my blog article (referenced in my answer) to try it out. – Airsource Ltd Jan 24 '20 at 12:28
0

I got a sufficient result reseting the cached response for a specific URL, changing the cache control to something that would never be returned like a "max-age=0" in the header. Look here

Community
  • 1
  • 1
Felipe Jun
  • 722
  • 11
  • 22
0

Whenever any API call is generated, That response is saved in the cache. You are able to find that folder in your documents directory, a folder with named cachedb is created which contains a list of responses. It can be a major concern related to security. With any third-party tool, someone can have access to that information.

Below is the way I have solved this issue :

NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:0 * 1024 * 1024 diskCapacity:0 * 1024 * 1024 diskPath:nil]; [NSURLCache setSharedURLCache:cache];

I have allocated 0 memory space to the cache. so no data will be stored in Cache memory.

PoojaArora
  • 31
  • 3
  • This was old issue in iOS8.... Maybe newer version this issue is not present... Thanks for response... – nkp Sep 03 '20 at 09:11