最近作者做的项目中需要用到UIWebView的离线缓存功能,本来满心欢喜的想着在UIWebView的代理方法中看看有没有什么代理方法可以直接做到缓存的功能,结果还是太天真了,后来网上搜索了一下(主要参考了在code4app上面rusking作业对UIWebView离线浏览的代码实现(地址https://github.com/lzhlewis2015/UIWebViewLocalCache),研究的过程中也花了不少时间,所以想在这里把我的心得分享一下),发现可以使用NSURLCache这个类实现。原理就是大多数的网络请求都会先调用这个类中的- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request 这个方法,那我们只要重写这个类,就能达到本地缓存的目的了。
下面是大致的逻辑
1 判断请求中的request 是不是使用get方法,据资料显示一些本地请求的协议也会进到这个方法里面来,所以在第一部,要把不相关的请求排除掉。
2 判断缓存文件夹里面是否存在该文件,如果存在,继续判断文件是否过期,如果过期,则删除。如果文件没有过期,则提取文件,然后组成NSCacheURLResponse返回到方法当中。
3在有网络的情况下,如果文件夹中不存在该文件,则利用NSConnection这个类发网络请求,再把返回的data和response 数据本地化存储起来,然后组成NSCacheURLResponse返回到方法当中。
4其中BaseTools和其他没有在本.m文件中定义的类为常用的工具类,这里不一一展开了。
大致逻辑就这么多,话不多说,直接看代码实现(关键代码有注释):
#import "CustomURLCache.h"
#import "NSObject+Network.h"
#import "BaseTools.h"
@interface CustomURLCache(private)
- (NSString *)cacheFolder;
- (NSString *)cacheFilePath:(NSString *)file;
- (NSString *)cacheRequestFileName:(NSString *)requestUrl;
- (NSString *)cacheRequestOtherInfoFileName:(NSString *)requestUrl;
- (NSCachedURLResponse *)dataFromRequest:(NSURLRequest *)request;
- (void)deleteCacheFolder;
@end
@implementation CustomURLCache
- (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path cacheTime:(NSInteger)cacheTime {
if (self = [super initWithMemoryCapacity:memoryCapacity diskCapacity:diskCapacity diskPath:path]) {
//cacheTime 为你所希望本地缓存的时间(以秒计算,如果设为60,则60秒之后本地缓存文件过期)
self.cacheTime = cacheTime;
if (path)
self.diskPath = path;
else
self.diskPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
self.responseDictionary = [NSMutableDictionary dictionaryWithCapacity:0];
}
return self;
}//
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
// 这里判断如果请求方法不为GET的话 直接返回父类方法,系统本来怎么干的就让它怎么干
if ([request.HTTPMethod compare:@"GET"] != NSOrderedSame) {
return [super cachedResponseForRequest:request];
}
// 核心方法
return [self dataFromRequest:request];
}//
- (void)removeAllCachedResponses {
[super removeAllCachedResponses];
[self deleteCacheFolder];
}//
- (void)removeCachedResponseForRequest:(NSURLRequest *)request {
[super removeCachedResponseForRequest:request];
NSString *url = request.URL.absoluteString;
NSString *fileName = [self cacheRequestFileName:url];
NSString *otherInfoFileName = [self cacheRequestOtherInfoFileName:url];
NSString *filePath = [self cacheFilePath:fileName];
NSString *otherInfoPath = [self cacheFilePath:otherInfoFileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:filePath error:nil];
[fileManager removeItemAtPath:otherInfoPath error:nil];
}//
#pragma mark - custom url cache
- (NSString *)cacheFolder {
return @"URLCACHE";
}//
- (void)deleteCacheFolder {
NSString *path = [NSString stringWithFormat:@"%@/%@", self.diskPath, [self cacheFolder]];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:path error:nil];
}//
- (NSString *)cacheFilePath:(NSString *)file {
NSString *path = [NSString stringWithFormat:@"%@/%@", self.diskPath, [self cacheFolder]];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
if ([fileManager fileExistsAtPath:path isDirectory:&isDir] && isDir) {
} else {
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
return [NSString stringWithFormat:@"%@/%@", path, file];
}//
- (NSString *)cacheRequestFileName:(NSString *)requestUrl {
//对传进来的url进行md5 加密 ,加密后变成32位字符串,作为文件名保存
return [BaseTools md5Hash:requestUrl];
}//
- (NSString *)cacheRequestOtherInfoFileName:(NSString *)requestUrl {
//同上
return [BaseTools md5Hash:[NSString stringWithFormat:@"%@-otherInfo", requestUrl]];
}//
- (NSCachedURLResponse *)dataFromRequest:(NSURLRequest *)request {
//此为GET的情况
// 这方法会返回多次 每一次链接相同的url(有网络的情况下,部分网页如:(百度),它这个url里面可能内嵌了很多其他的url,那其他的url可能每次都不一样,所以返回的request.url.absluteString 都不一样,这样导致每次系统会根据absoluteStr 来创建文件,则会越来越多;但针对作业的App里面涉及到的网页链接不会这样。
NSString *url = request.URL.absoluteString;
//md5 加密
NSString *fileName = [self cacheRequestFileName:url];
NSString *otherInfoFileName = [self cacheRequestOtherInfoFileName:url];
//filePath 用于保存网页数据
NSString *filePath = [self cacheFilePath:fileName];
//otherInfoPath 用于保存该url 对应的一些配置属性,如创建时间,MIMEType等。。
NSString *otherInfoPath = [self cacheFilePath:otherInfoFileName];
NSDate *date = [NSDate date];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
// expire 为过期的
BOOL expire = false;
NSDictionary *otherInfo = [NSDictionary dictionaryWithContentsOfFile:[otherInfoPath stringByAppendingString:@".plist"]];
//cacheTime 为磁盘缓存文件在硬盘中保存的时间
//cacheTime 为0 时则永远不会过期
if (self.cacheTime > 0) {
NSInteger createTime = [[otherInfo objectForKey:@"time"] intValue];
if (createTime + self.cacheTime < [date timeIntervalSince1970]) {
expire = true;
}
}
if (expire == false ) {
NSLog(@"data from cache ...");
//发现缓存文件夹里面有缓存在硬盘的文件
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:request.URL
MIMEType:[otherInfo objectForKey:@"MIMEType"]
expectedContentLength:data.length
textEncodingName:[otherInfo objectForKey:@"textEncodingName"]];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data] ;
return cachedResponse;
} else {
NSLog(@"cache expire ... ");
//过期了要删除
[fileManager removeItemAtPath:filePath error:nil];
[fileManager removeItemAtPath:[NSString stringWithFormat:@"%@",[otherInfoPath stringByAppendingString:@".plist"]] error:nil];
}
}
if (![self isReachability]) {
return nil;
}
// 有网络的状态下进行内容的缓存
__block NSCachedURLResponse *cachedResponse = nil;
//sendAsynchronousRequest请求也要经过NSURLCache
//如果没有response 和data 的话, 那字典对应的value 为true,方法直接返回nil(此链接不能使用缓存);
id boolExsit = [self.responseDictionary objectForKey:url];
if (boolExsit == nil) {
[self.responseDictionary setValue:[NSNumber numberWithBool:TRUE] forKey:url];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data,NSError *error)
{
// 如果有data 和response 返回的话
if (response && data) {
//因为cachesResponse 这个方法会被调用多次,所有dataFromrequest也会被调用多次,那如果服务器返回有response 和data的话,就把responDicionary 这个字典清空,并把对应的data写入,并把对应的data和response 构建成Cacheresponse 返回
[self.responseDictionary removeObjectForKey:url];
if (error) {
NSLog(@"error : %@", error);
NSLog(@"not cached: %@", request.URL.absoluteString);
cachedResponse = nil;
}
NSLog(@"---");
//save to cache
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f", [date timeIntervalSince1970]], @"time",
response.MIMEType, @"MIMEType",
response.textEncodingName, @"textEncodingName", nil];
BOOL dictSuccess = [dict writeToFile:[otherInfoPath stringByAppendingString:@".plist"] atomically:YES];
BOOL dataSuccess = [data writeToFile:filePath atomically:YES];
if (!dictSuccess) {
NSLog(@"字典失败");
}
if (!dataSuccess) {
NSLog(@"data 失败");
}
cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data] ;
}
}];
return cachedResponse;
}
return nil;
} //
@end
鉴于挺多读者可能看不到demo的下载地址,这里再列一下