【问题标题】:Disable UIWebView DiskImageCache禁用 UIWebView DiskImageCache
【发布时间】:2013-05-07 15:57:19
【问题描述】:

当加载包含图像的文档(例如 Microsoft Word docx 文件)时,无论缓存策略如何,UIWebView 都会在收到内存警告时缓存图像。

下面是示例代码sn-p:

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];

这种情况下,如果出现内存警告,会在app的tmp目录下新建一个文件夹,通常命名为DiskImageCache-random_suffix,所有图片都在打开的文档保存在这里。

UIWebView 加载新请求后,如果我调用

[sharedCache removeAllCachedResponses];

这些临时图像已被删除。

防止缓存图像的唯一方法是调用

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

但这意味着使用私有 API。

是否有“Apple 友好”的方式来实现这一点?

【问题讨论】:

    标签: ios objective-c caching uiwebview docx


    【解决方案1】:

    我在查看 WebKit “未记录”的偏好后发现了这一点。通过以下设置,可以在应用程序的整个生命周期内禁用 DiskImageCache:

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

    【讨论】:

    • 我刚刚用 iOS 7.1.1 测试了这个解决方案,并将示例代码上传到 GitHub。请注意,该设置将在您下次启动应用程序时,即初始化 UIWebView 时生效。
    • @DrMickeyLauer,试试 WebKitOfflineWebApplicationCacheEnabled
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2012-07-19
    • 2023-03-24
    • 2011-01-07
    • 2016-03-07
    • 1970-01-01
    相关资源
    最近更新 更多