Clear the Cache button's Click event
-(void) putbufferbtnclicked: (UIButton *) btn{
CGFloat size = [self Foldersizeatpath:nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES ). Lastobject] + [self foldersizeatpath:nssearchpathfordirectoriesindomains (nslibrarydirectory, NSUserDomainMask, YES). Lastobject] + [self foldersizeatpath:nstemporarydirectory ()];
NSString *message = size > 1? [NSString stringwithformat:@ "Cache%.2fm, delete cache", size]: [NSString stringwithformat:@ "Cache%.2fk, delete cache", size * 1024.0];
Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:nil message:message PreferredStyle: ( Uialertcontrollerstylealert)];
Uialertaction *action = [uialertaction actionwithtitle:@ "OK" style: (Uialertactionstyledefault) handler:^ ( Uialertaction *action) {[Self cleancaches]; }];
Uialertaction *cancel = [uialertaction actionwithtitle:@ "Cancel" style: (Uialertactionstylecancel) Handler:nil];
[Alert addaction:action];
[Alert Addaction:cancel];
[Self Showdetailviewcontroller:alert sender:nil];
}
Calculate Directory Size
-(CGFloat) Foldersizeatpath: (NSString *) path{
Using Nsfilemanager to achieve the management of files
Nsfilemanager *manager = [Nsfilemanager Defaultmanager];
CGFloat size = 0;
if ([manager Fileexistsatpath:path]) {
Get the file under the directory, calculate its size
Nsarray *childrenfile = [manager Subpathsatpath:path];
For (NSString *filename in Childrenfile) {
NSString *absolutepath = [path stringbyappendingpathcomponent:filename];
Size + = [Manager Attributesofitematpath:absolutepath error:nil].filesize;
}
Convert Size to M
return size/1024.0/1024.0;
}
return 0;
}
Delete files based on path
-(void) Cleancaches: (NSString *) path{
Using Nsfilemanager to achieve the management of files
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
if ([FileManager Fileexistsatpath:path]) {
Get the file name below the path
Nsarray *childrenfiles = [FileManager Subpathsatpath:path];
For (NSString *filename in childrenfiles) {
Stitching Path
NSString *absolutepath = [path stringbyappendingpathcomponent:filename];
Delete a file
[FileManager Removeitematpath:absolutepath Error:nil];
}
}
}
To calculate the size of the files in the sandbox and delete the files in the sandbox example:
-(void) cleancaches{
[Self Cleancaches:nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES). FirstObject];
[Self Cleancaches:nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES). FirstObject];
[Self cleancaches:nstemporarydirectory ()];
}
iOS Clear Cache