OGeek|极客世界-中国程序员成长平台

标题: ios - 如何为所有 session 缓存 UIWebView 页面 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:42
标题: ios - 如何为所有 session 缓存 UIWebView 页面

当页面没有变化时,我想第一次从网络加载页面,其他时间从缓存加载。

我的缓存有一些问题。

我将此代码粘贴到 AppDelegate 中:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
                                                        diskCapacity:100 * 1024 * 1024
                                                            diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

这就是我使用请求的方式:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:dataURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:2*60];

如果我留在应用程序中,它可以正常工作并从缓存中加载页面,但是当我关闭我的应用程序并再次加载它时,页面会再次从网络加载。我必须做什么,从缓存中加载它?



Best Answer-推荐答案


SDURLCache 是 NSURLCache 的替代品,但在 iOS 4 及更高版本上会保存到磁盘。

- (void)prepareCache {
    SDURLCache *cache = [[SDURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                      diskCapacity:20 * 1024 * 1024
                                                          diskPath:[SDURLCache defaultCachePath]];
    cache.minCacheInterval = 0;
    [NSURLCache setSharedURLCache:cache];
    NSLog(@"Cache is being logged to: %@", [SDURLCache defaultCachePath]);
}

更多信息可以找到here:

关于ios - 如何为所有 session 缓存 UIWebView 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32502562/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4