在iOS 8上共享NSURLCache和UIWebView [英] Shared NSURLCache and UIWebView on iOS 8
问题描述
在iOS 7中,我能够将共享URL缓存设置为 NSURLCache
的子类以及任何 UIWebView
我创建的会自动为每个请求使用该共享缓存。
In iOS 7, I was able to set a shared URL cache to a subclass of NSURLCache
and any UIWebView
s 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];
但是,现在在iOS 8中,似乎UIWebView从共享缓存中拉出来并且 cachedResponseForRequest
永远不会被调用。
However, now in iOS 8 it doesn't seem like UIWebView pulls from the shared cache and cachedResponseForRequest
never gets called.
是否有人找到此更改的文档或解决方法?
Has anyone found documentation for this change, or a workaround?
推荐答案
我今天遇到了同样的问题。它在ios7上没问题,在ios8上坏了。
I had same problem today. It was ok on ios7 and broken on ios8.
诀窍是在didFinishLaunchingWithOptions中创建自己的缓存。
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];
...
您可以在其他应用中看到这一点:
You can see this being done in other apps:
https: //github.com/AFNetworking/AFNetworking/blob/master/Example/AppDelegate.m
这个网站虽然陈旧,却有更多关于你为何的信息甚至不应该在上面的代码之前调用[NSURLCache sharedInstance]:
http:// inessential .com / 2007/02/28 / figured_it_the_heck_out
This site, while old, has more info on why you shouldn't even call [NSURLCache sharedInstance] before the above code: http://inessential.com/2007/02/28/figured_it_the_heck_out
这篇关于在iOS 8上共享NSURLCache和UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!