微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在iOS 8上共享NSURLCache和UIWebView

在iOS 7中,我能够将共享URL缓存设置为NSURLCache的子类,我创建的任何UIWebViews都会自动为每个请求使用该共享缓存.
// Set the URL cache and leave it set permanently
ExampleURLCache *cache = [[ExampleURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];

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

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

解决方法

我今天遇到了同样的问题.它在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];

...

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐