UIWebView缓存基础知识
2016-05-24 00:00
141 查看
第一. NSURLRequest的缓存设置
- (id)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
cachePolicy表示缓存策略
1、 NSURLRequestUseProtocolCachePolicy = 0,
默认策略,使用缓存
2.NSURLRequestReloadIgnoringLocalCacheData = 1,
忽略本地缓存
3.NSURLRequestReturnCacheDataElseLoad = 2,
如果有缓存,不管过期时间优先使用本地缓存,如果没有本地缓存,才从原地址下载
4. NSURLRequestReturnCacheDataDontLoad = 3
只使用缓存,如果没有匹配的缓存则报告离线模式,而不会从网上load数据
第二、NSURLCache
1. 初始化相关的几个方法:sharedURLCache;setSharedURLCache;initWithMemoryCapacity
sharedURLCache方法返回一个NSURLCache实例。
默认情况下,内存是4M,4* 1024 * 1024;Disk为20M,20 * 1024 * 1024;路径在(NSHomeDirectory)/Library/Caches/(current application name, [[NSProcessInfo processInfo] processName])
setSharedURLCache可以通过这个方法来改变默认的NSURLCache。通过initWithMemoryCapacity来定制自己的NSURLCache。
2.常用的几个函数:
//Returns the NSCachedURLResponse stored in the cache with the given request.
(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
//Stores the given NSCachedURLResponse in the cache using the given request.
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;
// Removes the NSCachedURLResponse from the cache that is stored using the given request.
(void)removeCachedResponseForRequest:(NSURLRequest *)request;
//Clears the given cache, removing all NSCachedURLResponse objects that it stores.
- (void)removeAllCachedResponses;
3. property方法
- (NSUInteger)memoryCapacity;
- (NSUInteger)diskCapacity;
- (void)setMemoryCapacity:(NSUInteger)memoryCapacity;
第三、 NSCachedURLResponse
系统缓存对象,保持了缓存对象的个性和特性。
1. NSURLCacheStoragePolicy 缓存策略有三种
enum
{
NSURLCacheStorageAllowed,
NSURLCacheStorageAllowedInMemoryOnly,
NSURLCacheStorageNotAllowed,
};
NSURLCacheStorageAllowed
Specifies that storage in NSURLCache is allowed without restriction.
Important: iOS prior to version 5 ignores this cache policy, and instead treats it asNSURLCacheStorageAllowedInMemoryOnly.
可以看出,iOS设备上NSURLCache默认只能进行内存缓存。
2. 构造方法
- (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data;
- (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data userInfo:(NSDictionary *)userInfo storagePolicy:(NSURLCacheStoragePolicy)storagePolicy;
3. Open API
- (NSURLResponse *)response;
- (NSData *)data;
- (NSDictionary *)userInfo;
- (NSURLCacheStoragePolicy)storagePolicy;
- (id)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
cachePolicy表示缓存策略
1、 NSURLRequestUseProtocolCachePolicy = 0,
默认策略,使用缓存
2.NSURLRequestReloadIgnoringLocalCacheData = 1,
忽略本地缓存
3.NSURLRequestReturnCacheDataElseLoad = 2,
如果有缓存,不管过期时间优先使用本地缓存,如果没有本地缓存,才从原地址下载
4. NSURLRequestReturnCacheDataDontLoad = 3
只使用缓存,如果没有匹配的缓存则报告离线模式,而不会从网上load数据
第二、NSURLCache
1. 初始化相关的几个方法:sharedURLCache;setSharedURLCache;initWithMemoryCapacity
sharedURLCache方法返回一个NSURLCache实例。
默认情况下,内存是4M,4* 1024 * 1024;Disk为20M,20 * 1024 * 1024;路径在(NSHomeDirectory)/Library/Caches/(current application name, [[NSProcessInfo processInfo] processName])
setSharedURLCache可以通过这个方法来改变默认的NSURLCache。通过initWithMemoryCapacity来定制自己的NSURLCache。
2.常用的几个函数:
//Returns the NSCachedURLResponse stored in the cache with the given request.
(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
//Stores the given NSCachedURLResponse in the cache using the given request.
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;
// Removes the NSCachedURLResponse from the cache that is stored using the given request.
(void)removeCachedResponseForRequest:(NSURLRequest *)request;
//Clears the given cache, removing all NSCachedURLResponse objects that it stores.
- (void)removeAllCachedResponses;
3. property方法
- (NSUInteger)memoryCapacity;
- (NSUInteger)diskCapacity;
- (void)setMemoryCapacity:(NSUInteger)memoryCapacity;
第三、 NSCachedURLResponse
系统缓存对象,保持了缓存对象的个性和特性。
1. NSURLCacheStoragePolicy 缓存策略有三种
enum
{
NSURLCacheStorageAllowed,
NSURLCacheStorageAllowedInMemoryOnly,
NSURLCacheStorageNotAllowed,
};
NSURLCacheStorageAllowed
Specifies that storage in NSURLCache is allowed without restriction.
Important: iOS prior to version 5 ignores this cache policy, and instead treats it asNSURLCacheStorageAllowedInMemoryOnly.
可以看出,iOS设备上NSURLCache默认只能进行内存缓存。
2. 构造方法
- (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data;
- (id)initWithResponse:(NSURLResponse *)response data:(NSData *)data userInfo:(NSDictionary *)userInfo storagePolicy:(NSURLCacheStoragePolicy)storagePolicy;
3. Open API
- (NSURLResponse *)response;
- (NSData *)data;
- (NSDictionary *)userInfo;
- (NSURLCacheStoragePolicy)storagePolicy;
相关文章推荐
- SEVERE: Error configuring application listener of
- [iOS] 使用WebViewJavascriptBridge实现OC与JS交互
- Unity游戏画面品质增强,shader和贴图
- Swift专题讲解十七——Optional链
- Android DrawerLayout 侧滑菜单栏
- 最新android APP框架介绍
- Android RxJava使用介绍(二) RxJava的操作符
- iOS开发用到的Mac系统资源库中各种文件的路径
- Java+Android实现DES与四种模式、AES、MD5算法三合一
- ViewDragHelper源码分析
- android开发笔记之Json解析
- Android三级图片缓存
- [Android]Android开源工具项目集合20160523
- 关于handler机制(结合源码及方法的调用去总结)
- android源码解析(二十七)-->HOME事件流程
- 第一个Android crackme(2016-05)
- Android-architecture之MVC、MVP、MVVM、Data-Binding
- Android RxJava使用介绍(一)概念
- android studio 2.0 导入工程
- android中的各类权限