iOS-app清除缓存
一直寻寻觅觅找app的清除缓存的方法,发现:并没有什么固定的方法,你既然有做对应的缓存机制,这个机制就应该有清除缓存的方法。例如如果你使用某个第三方的图片库,这个库有缓存机制,那么它就应该提供对应的清除缓存的方法。你调用对应的方法进行清除,如果你自己有用到数据库,那么你就应该清除数据库里面的数据等等。有的公司需求是显示缓存大小,有的不需要:
@property ( nonatomic , strong ) UILabel * cachLabel;//显示缓存有多少m
cell.cachLabel . text = [ NSString stringWithFormat : @"(%.2fM)" , [ self filePath ]];
// 显示缓存大小
- ( float )filePath
{
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
return [ self folderSizeAtPath :cachPath];
}
不需要显示缓存大小M的:
//iOS8以上的版本使用
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清除缓存?"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//清除缓存所需要做的处理
[self clearFile];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:okAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
#pragma mark ---action---
//1:首先我们计算一下 单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
NSFileManager * manager = [ NSFileManager defaultManager ];
if ([manager fileExistsAtPath :filePath]){
return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
}
return 0 ;
}
//2: 遍历文件夹获得文件夹大小,返回多少 M(提示:你可以在工程界设置()m)
- ( float ) folderSizeAtPath:( NSString *) folderPath{
NSFileManager * manager = [ NSFileManager defaultManager ];
if (![manager fileExistsAtPath :folderPath]) return 0 ;
NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
NSString * fileName;
long long folderSize = 0 ;
while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
folderSize += [ self fileSizeAtPath :fileAbsolutePath];
}
return folderSize/( 1024.0 * 1024.0 );
}
// 清理缓存
- ( void )clearFile
{
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
NSLog ( @"cachpath = %@" , cachPath);
for ( NSString * p in files) {
NSError * error = nil ;
NSString * path = [cachPath stringByAppendingPathComponent :p];
if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
[[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
}
}
[ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject :nil waitUntilDone : YES ];
}
- ( void )clearCachSuccess
{
NSLog ( @" 清理成功 " );
//iOS8以上的版本使用
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"缓存清理完毕"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
[self.tableView reloadInputViews];//清理完之后重新导入数据
}
给app评论功能实现
//评价
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1017244804&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"]];
iOS-app清除缓存的更多相关文章
- iOS之清除缓存
//清除缓存按钮的点击事件 - (void)putBufferBtnClicked:(UIButton *)btn{ CGFloat size = [self folderSizeAtPath:NSS ...
- iOS UIWebView清除缓存
UIWebView清除Cookie: //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieSto ...
- iOS中清除缓存的方法 以及SDWebimage自带的清除缓存方法
1 SDWebimage中 (1) 计算缓存的大小 单位 : (MB) CGFloat size = [[SDImageCache sharedImageCache] getSize] / 102 ...
- ios开发清除SDWebImage图片缓存
一:一般在实际的项目应用中都会用到第三方框架SDWebImage去下载缓存图片,但在ios开发应用中,常常涉及对SDWebImage缓存图片的清除.本文所列出代码即是对SDWebImage缓存图片的清 ...
- iOS 清除缓存
iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒.它包含三个文件夹: Documents: 苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下 ...
- 关于app的清除缓存的功能
一直寻寻觅觅找app的清除缓存的方法,发现:并没有什么固定的方法,你既然有做对应的缓存机制,这个机制就应该有清除缓存的方法.例如如果你使用某个第三方的图片库,这个库有缓存机制,那么它就应该提供对应的清 ...
- iOS开发获取缓存文件的大小并清除缓存
移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯.购物.阅读类 ...
- iOS开发 - Swift实现清除缓存功能
前言: 开发移动应用时,请求网络资源是再常见不过的功能.如果每次都去请求,不但浪费时间,用户体验也会变差,所以移动应用都会做离线缓存处理,其中已图片缓存最为常见. 但是时间长了,离线缓存会占用大量的手 ...
- iOS开发 -李洪强-清除缓存
// // SetViewController.m // dfhx // // Created by dfhx_iMac_001 on 16/4/5. // Copyright © 2016年 ...
- iOS清除缓存功能开发
在APP开发中,大量的图片或消息占用系统内存,造成一堆垃圾信息,所以开发清除缓存功能就显得必不可少了. 代码段1:获取文件的大小 - (long long) fileSizeAtPath:(NSStr ...
随机推荐
- concat以及group_concat的用法
concat()函数 1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null ...
- STM32F10XX学习笔记的石墨连接
https://shimo.im/docs/QHGRrWxbeb0NiBm9/ <STM32F10X系列笔记>,可复制链接后用石墨文档 App 打开
- [Dart] Manipulate Lists/Arrays in Dart
We will learn how to work with Lists using a variety of methods made available in the dart:core libr ...
- LaTex编译时出错:PK font *** could not be created
参考https://blog.csdn.net/dancing11/article/details/60978642 在用TeXworks (Miktex) 编译OSA投稿模板时,遇到错误PK fon ...
- P5541 [USACO19FEB]Sleepy Cow Herding
ri,被黄题虐. 思路:贪心?? 提交:2次 错因:没有特判 题解: 先排序. 最小代价:固定区间长度为\(n\),我们扫一遍数组看区间最多包含几个数,设为 \(mx\) ,答案就是\(n-mx+1\ ...
- maven-pom文件的scope作用域
1.compile 编译范围,默认scope,在工程环境的classpath(编译环境)和打包(如果是WAR包,会包含在WAR包中)时候都有效. 2.provided 容器或JDK已提供范围,表示该依 ...
- linux中的set ff=unix
set ff=unix : 告诉 vi 编辑器,使用unix换行符. 操作步骤: 1.用vi命令打开文件 2.直接输入 :set ff=unix
- 002_simulink中显示模块中的名字
(一)Display--->Hide Automatic Names(√去掉)
- Linux操作系统常用命令合集——第四篇-文件系统权限操作(5个命令)
1.umask [命令作用] 文件或目录创建时的遮罩码 [命令语法] umask [选项] [参数] [常用选项] -p --输出的权限掩码可直接作为指令来执行 -s --以符号方式 ...
- 10分钟教你用eclipse上传代码到GitHub
关注我们的公众号哦!获取更多精彩消息! 好久没有更新了,这两天小编在整理以前的代码,上传到GitHub做备份. 加上现在GitHub的私有仓库不是免费了嘛,所以今天顺便给大家讲讲怎么用eclipse上 ...