在iOS 8上共享NSURLCache和UIWebView

Jon*_*lis 6 uiwebview nsurlcache ios8

在iOS 7中,我能够将共享URL缓存设置为子类,NSURLCache并且UIWebView我创建的任何s都会自动为每个请求使用该共享缓存.

// Set the URL cache and leave it set permanently
ExampleURLCache *cache = [[ExampleURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];
Run Code Online (Sandbox Code Playgroud)

但是,现在在iOS 8中,似乎UIWebView从共享缓存中拉出并且cachedResponseForRequest永远不会被调用.

有没有人找到这个变化的文档,或者解决方法?

Yeg*_*sky 10

我今天遇到了同样的问题.它在ios7上没问题,在ios8上坏了.

诀窍是创建自己的缓存,这是你在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];

...
Run Code Online (Sandbox Code Playgroud)

您可以在其他应用中看到此操作:

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

这个网站虽然陈旧,却有更多信息说明为什么你不应该在上面的代码之前调用[NSURLCache sharedInstance]:http://inessential.com/2007/02/28/figured_it_the_heck_out