iOS中,关于UIWebView网页数据本地缓存原理和实际使用。

     最近作者做的项目中需要用到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的下载地址,这里再列一下

https://github.com/lzhlewis2015/UIWebViewLocalCache

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容