app中關(guān)于網(wǎng)頁用的是越來越多了,所以有關(guān)網(wǎng)頁緩存等問題就出現(xiàn)了?
怎么進(jìn)行緩存,獲取緩存的大小,獲取緩存數(shù)據(jù),清理緩存等問題就一個一個地出現(xiàn)了。針對這個問題,通過查找前人的博客簡書等得到了答案,鑒于不是完整的解決方案,所以進(jìn)行了整理,代碼都是經(jīng)過鄙人驗證的,如有出入還請大家指正,謝謝!
1.先從存寫起吧
/**
* 網(wǎng)頁緩存寫入文件
*/
+ (void)writeToCache:(NSString *)fileName
{
NSString *htmlResponseStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:fileName] encoding:NSUTF8StringEncoding error:nil];
//創(chuàng)建文件管理器
NSFileManager *fileManager = [[NSFileManager alloc]init];
//獲取document路徑
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[fileManager createDirectoryAtPath:[cachesPath stringByAppendingString:@"/Caches"]withIntermediateDirectories:YES attributes:nil error:nil];
//寫入路徑
NSString *path = [cachesPath stringByAppendingString:[NSString stringWithFormat:@"/Caches/%lu",(unsigned long)[fileName hash]]];
[htmlResponseStr writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
2.可以存了,那么怎么獲取呢
/**
* 獲取網(wǎng)頁緩存文件
* fileName 文件名
*/
+ (NSString *)readFromCache:(NSString *)fileName{
// 判斷是否從緩存中獲取
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *path = [cachesPath stringByAppendingString:[NSString stringWithFormat:@"/Caches/%lu",(unsigned long)[fileName hash]]];
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
return htmlString;
}
3.那么緩存的大小是多少呢?
/**
* 獲取緩存文件大小
*/
+ (CGFloat)getCacheSize{
//獲取文件管理器對象
NSFileManager * fileManger = [NSFileManager defaultManager];
//獲取緩存沙盒路徑
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//拼接緩存文件文件夾路徑
NSString * fileCachePath = [cachePath stringByAppendingPathComponent:@"/Caches"];
//獲取到該緩存目錄下的所有子文件(只是文件名并不是路徑,后面要拼接)
NSArray * subFilePath = [fileManger subpathsAtPath:fileCachePath];
//先定義一個緩存目錄總大小的變量
NSInteger fileTotalSize = 0;
for (NSString * fileName in subFilePath)
{
//拼接文件全路徑(注意:是文件)
NSString * filePath = [fileCachePath stringByAppendingPathComponent:fileName];
//獲取文件屬性
NSDictionary * fileAttributes = [fileManger attributesOfItemAtPath:filePath error:nil];
//根據(jù)文件屬性判斷是否是文件夾(如果是文件夾就跳過文件夾,不將文件夾大小累加到文件總大?。? if ([fileAttributes[NSFileType] isEqualToString:NSFileTypeDirectory]) continue;
//獲取單個文件大小,并累加到總大小
fileTotalSize += [fileAttributes[NSFileSize] integerValue];
}
//將字節(jié)大小轉(zhuǎn)為MB,然后傳出去
return fileTotalSize/1024.0/1024.0;
}
4.是的,緩存要可以清除的
/**
* 清除文件緩存
*/
+ (void)cleanCacheSuccess:(void (^)())success failure:(void (^)())failure{
//獲取文件管理器對象
NSFileManager * fileManger = [NSFileManager defaultManager];
//獲取緩存沙盒路徑
NSString * cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//拼接緩存文件文件夾路徑
NSString * fileCachePath = [cachePath stringByAppendingPathComponent:@"/Caches"];
//獲取到該緩存目錄下的所有子文件(只是文件名并不是路徑,后面要拼接)
NSArray * subFilePath = [fileManger subpathsAtPath:fileCachePath];
NSError *error = nil;
for (NSString *p in subFilePath) {
NSString *path = [fileCachePath stringByAppendingPathComponent:p];
if ([fileManger fileExistsAtPath:path]) {
[fileManger removeItemAtPath:path error:&error];
if (error) {
break;
}
}
}
if (error) {
if (failure) failure();
}else{
if (success) success();
}
}
5.為什么不寫一個工具類呢,對啊,代碼地址:https://github.com/silenceXz/WebCacheTool
如何使用小聊一下,下載代碼并導(dǎo)入項目中,然后在使用的地方引入#import "CacheFileManagerTool.h"即可。
比如加載網(wǎng)頁,需要知道網(wǎng)頁地址self.urlPath,先判斷是否有緩存,沒有就進(jìn)行網(wǎng)絡(luò)加載并寫入緩存,如果有就加載緩存文件。
NSURL *url = [NSURL URLWithString:self.urlPath];
// 如果有緩存,那么就從緩存中取得
NSString *htmlString = [CacheFileManagerTool readFromCache:self.urlPath];
if(!(htmlString == nil || [htmlString isEqualToString:@""])){
[self.webHtml loadHTMLString:htmlString baseURL:url];
}else{
[self.webHtml loadRequest:[NSURLRequest requestWithURL:url]];
}