iOS cleanup cache

Source: Internet
Author: User

//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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.