iOS-UIWebview缓存并保证实时性

来源:互联网 发布:腾讯云阿里云哪个好 编辑:程序博客网 时间:2024/05/16 16:24

一.缓存需求

资源文件没有更新,只加载本利缓存文件;资源文件发生改变或者更新,第一时间使用新的文件。

二.缓存策略

1.NSURLRequestUseProtocolCachePolicy = 0,
默认策略,使用缓存

2.NSURLRequestReloadIgnoringLocalCacheData = 1,
忽略本地缓存

3.NSURLRequestReturnCacheDataElseLoad = 2,
如果有缓存,不管过期时间优先使用本地缓存,如果没有本地缓存,才从原地址下载

4.NSURLRequestReturnCacheDataDontLoad = 3
只使用缓存,如果没有匹配的缓存则报告离线模式,而不会从网上load数据

我认为NSURLRequestUseProtocolCachePolicy是最接近我们的缓存需求的,经我理解我认为他的缓存流程如下:


注意:1.在判断是否过期的时候,如果我们没有设置过期时间,这缓存的文件就会立即过期,就会直接请求加载数据;

   2.当网络环境差的时候,缓存已经过期,在我们向服务器询问是否更新的时候,就会返回304,又会走缓存。如果发出询问是否更新耗时过长,则会造成白屏。

三.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;

五.Application Cache


这个是h5中用到的一个缓存方式。使用manifest配置文件,告知客户端哪些需要更新,哪些不需要更新。使用这种方式,需要枚举出所有需要缓存的配置文件,每次打开页面都要去请求配置文件是否有更新,如果配置文件有更新,则更新配置文件中列出的资源文件。 
缺点:多个页面引用同一个js,将会缓存两份js文件。需要服务端配合 
优点:快速的展现缓存的内容,后台去更新缓存,下次再打开该资源时将使用最新的。

六.ZFJUrlCacheConfig(自定义缓存机制)

1.缓存机制

既可以快速加载缓存内容,又可以实时更新,又可以防止网络差导致的白屏。和Application Cache缓存机制差不多,快速加载缓存,APP后台异步去更新,然后再去更新页面的数据。

流程图如下:


3.使用教程

开始监听:[ZFJCacheURLProtocol startListeningNetWorking];

取消监听:[ZFJCacheURLProtocol cancelListeningNetWorking];

注意:我们在不用UIwebview的时候,一定要取消监听,应为监听的是所以得网络请求。

4.DEMO演示

第一张是未开启缓存,第二种是开启缓存


5.ZFJCacheURLProtocol下载

点击下载

http://download.csdn.net/detail/u014220518/9844744


相关阅读:

1.HTML5应用程序缓存Application Cache

2.AFN的坑--NSCachedURLResponse缓存






原创粉丝点击