Disable UIWebView DiskImageCache

When loading documents containing images (for example, a Microsoft Word docx file), UIWebView will always cache images when it receives a memory warning, regardless of cache policy.

The following is an example code snippet:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:0 diskPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"URLCache"]]; [NSURLCache setSharedURLCache:sharedCache]; NSURLRequest* req = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.its.swinburne.edu.au/about/projects/templates/TechnicalSpecificationTemplatev1.1-[ProjectName]-[ver]-[YYYYMMDD].docx"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; 

In these circumstances, when a memory warning occurs, a new folder is created in the tmp directory of the application, usually called DiskImageCache-random_suffix , and all the images in the open document are saved here.

After UIWebView loads a new request, if I call

 [sharedCache removeAllCachedResponses]; 

these temporary images are deleted.

The only way to prevent image caching is to call

 [NSClassFromString(@"WebCache") performSelector:@selector(setDisabled:) withObject:[NSNumber numberWithBool:YES]]; 

but that means using a private API.

Is there a β€œfriendly Apple” way to achieve this?

+4
source share
1 answer

I realized this by looking at "undocumented" preferences in WebKit. The next time you configure, you can disable DiskImageCache throughout the life of the application:

 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"]; [[NSUserDefaults standardUserDefaults] synchronize]; 
+6
source

Source: https://habr.com/ru/post/1480508/


All Articles