//get to clean the path, in fact, is the path of the caches, generally like this many places will be used in a place really good to make a macro, but now Apple does not advocate the use of macros//constants that can be defined as global in Swift//traverse the caches, the internal file size is calculated, click to confirm the deletion of the words directly delete all files, if you do not want to clean the file, you can traverse the file according to the path filter out{nsstring*path =Wnxcachespath; Nsfilemanager*filemanager=[Nsfilemanager Defaultmanager]; floatfoldersize; if([FileManager Fileexistsatpath:path]) {//get an array of files that are countedNsarray *childerfiles =[FileManager Subpathsatpath:path]; //get the name of each file, if there is a file that you do not want to erase here to Judge for(NSString *filenameinchchilderfiles) { //stitching the paths togetherNSString *fullpath =[path Stringbyappendingpathcomponent:filename]; Foldersize+=[self filesizeatpath:fullpath]; } Self.alertview= [[Uialertview alloc] Initwithtitle:@"clean up the cache"Message:[nsstring stringWithFormat:@"Cache size is%.2FM, are you sure you want to clean up the cache ?", Foldersize]Delegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine", nil]; [Self.alertview show]; Self.alertview.Delegate=Self ; } }
1 #pragmaMark Uialertviewdelegate2- (void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) Buttonindex3 {4 if(buttonindex) {5 //Click OK to traverse the entire caches file and empty the cache inside6NSString *path =Wnxcachespath;7Nsfilemanager *filemanager=[Nsfilemanager Defaultmanager];8 if([FileManager Fileexistsatpath:path]) {9Nsarray *childerfiles=[FileManager Subpathsatpath:path];Ten for(NSString *filenameinchchilderfiles) { One //if necessary, add conditions to filter out files that you do not want to delete ANSString *absolutepath=[path stringbyappendingpathcomponent:filename]; - [FileManager Removeitematpath:absolutepath error:nil]; - } the } - } - -Self.alertview =Nil; + } - + //calculate the size of a single folder A-(float) Filesizeatpath: (NSString *) path{ at -Nsfilemanager *filemanager=[Nsfilemanager Defaultmanager]; - - if([FileManager Fileexistsatpath:path]) { - - Long LongSize=[FileManager attributesofitematpath:path error:nil].filesize; in - returnsize/1024.0/1024.0; to } + return 0; -}
iOS cleanup cache