iOS Clear Cache feature development

Source: Internet
Author: User

In app development, a large number of images or messages occupy the system memory, resulting in a heap of spam information, so the development of clear cache function is necessary.

Code Snippet 1: Get the file size

-(longlong) Filesizeatpath: (nsstring*) filepath{    nsfilemanager* manager = [ Nsfilemanager Defaultmanager];     if ([manager Fileexistsatpath:filepath]) {        return  [[Manager Attributesofitematpath: FilePath Error:nil] fileSize];    }     return 0 ;}

Code Snippet 2: Get the file size

//Traverse folder to get folder size, return how many M- (float) Foldersizeatpath: (nsstring*) folderpath{Nsfilemanager* Manager =[Nsfilemanager Defaultmanager]; if(! [Manager Fileexistsatpath:folderpath])return 0; Nsenumerator*childfilesenumerator =[[manager Subpathsatpath:folderpath] objectenumerator]; NSString*FileName; Long LongFoldersize =0;  while(FileName = [Childfilesenumerator nextobject])! =Nil) {NSString* Fileabsolutepath =[FolderPath Stringbyappendingpathcomponent:filename]; Foldersize+=[self filesizeatpath:fileabsolutepath]; }    returnfoldersize/(1024.0*1024.0);} 

Code Snippet 3: Clear Cache Function Method one

1. Clear the first type of cache- (void) Action: (ID) sender{//completely clear the cache the first methodUIButton* Button =Sender; [Button Settitle:@"clean up ."Forstate:uicontrolstatenormal]; Nsarray*paths =nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString*path =[Paths Lastobject]; NSString*STR = [NSString stringWithFormat:@"cache has been cleared%.1FM", [self foldersizeatpath:path]]; NSLog (@"%@", str); Nsarray*files =[[Nsfilemanager Defaultmanager] subpathsatpath:path];  for(NSString *pinchfiles) {Nserror*error; NSString*path =[path stringbyappendingpathcomponent:p]; if([[[Nsfilemanager Defaultmanager] Fileexistsatpath:path]) {[[Nsfilemanager Defaultmanager] RemoveItemAtPat H:path Error:&ERROR]; }    }}

Code Snippet 4: Clear Cache Function Method two

[[Sdimagecache Sharedimagecache] cleardisk]; [[Sdimagecache Sharedimagecache] clearmemory];

iOS Clear Cache feature development

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.