iOS Clear Cache

Source: Internet
Author: User

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

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.