Generic NSURLCache and UIWebView on iOS 8

In iOS 7, I was able to set a shared URL cache in a subclass of NSURLCache , and any UIWebView I created would automatically use that shared cache for each request.

 // Set the URL cache and leave it set permanently ExampleURLCache *cache = [[ExampleURLCache alloc] init]; [NSURLCache setSharedURLCache:cache]; 

However, now in iOS 8 it does not look like UIWebView is pulling out of the shared cache, and cachedResponseForRequest never called.

Has anyone found documentation for this change or workaround?

+6
source share
1 answer

I had the same problem today. This was normal on ios7 and broken on ios8.

The trick is to create your own cache, as the first thing you do in didFinishLaunchingWithOptions.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // IMPORTANT: call this line before anything else. Do not call [NSURLCache sharedCache] before this because that // creates a reference and then we can't create the new cache. NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil]; [NSURLCache setSharedURLCache:URLCache]; ... 

This can be done in other applications:

https://github.com/AFNetworking/AFNetworking/blob/master/Example/AppDelegate.m

This site, while old, has more information on why you shouldn't even call [NSURLCache sharedInstance] before the above code: http://inessential.com/2007/02/28/figured_it_the_heck_out

+10
source

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


All Articles