Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeAllCachedResponses can not clear sharedURLCache?

I made a button to clear my cache:

    [[NSURLCache sharedURLCache]removeAllCachedResponses];

after this being done, I check the size of sharedURLCache:

NSInteger sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage];
float sizeInMB = sizeInteger / (1024.0f * 1024.0f);

the sizeInMB is 0.17, sometimes 0.13. Never 0.0. Why the removeAllCachedResponses doesn't make the sharedURLCache to ZERO ?

ps: in AppDelegate.m didFinishLaunchingWithOptions:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                                    diskCapacity:20 * 1024 * 1024
                                                                        diskPath:nil];

    [NSURLCache setSharedURLCache:sharedCache];
like image 473
William Avatar asked May 26 '26 13:05

William


1 Answers

During pen test (security testing), I have got issue of caching url request/ response by iOS in cache.db.

I tried using

URLCache.shared.removeCachedResponse(for: URLRequest) and URLCache.shared.removeAllCachedResponses()

Both above API doesn't seems to be working just after getting response from API Call

Then got it working by putting following two line of code in didFinishLaunchingWithOptions in AppDelegate

// first remove all url cache from previous versions of app
URLCache.shared.removeAllCachedResponses()


// As we do not want to cache any request response so
// updated shared instance of url cache with custom url cache where
// memoryCapacity = 0, diskCapacity = 0 and diskPath = nil
URLCache.shared = URLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
like image 133
Surendra Kumar Avatar answered May 30 '26 04:05

Surendra Kumar