IOS get local cache file size and erase

Source: Internet
Author: User

Because the project needs to calculate the size of the cache file in the application and cleanup work, do a small module to share, casually say that buy apples or choose a 16g or more so mom will not worry about your memory is not enough oh!

clear Local cache file

+ (void) Clearcachefile

{

nsstring *cachpath = [nssearchpathfordirectoriesindomains(nscachesdirectory, Nsuserdomainmask, YES) objectatindex:0];

nsarray *files = [[nsfilemanager defaultmanager] subpathsatpath: Cachpath];

for (nsstring *filename in files)

{

nserror *error;

nsstring *path = [Cachpath stringbyappendingpathcomponent: fileName];

if ([[nsfilemanager defaultmanager] fileexistsatpath:p Ath])

{

[[nsfilemanager defaultmanager] removeitematpath:p ath error:&error];

}

}

}

calculate local cache file size

+ (double) getcachefilesize

{

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

double fileSize = 0.0;

nsstring *cachpath = [nssearchpathfordirectoriesindomains(nscachesdirectory, Nsuserdomainmask, YES) objectatindex:0];

nsarray *files = [FileManager subpathsatpath: Cachpath];

for (nsstring *filename in files)

{

nsstring *path = [Cachpath stringbyappendingpathcomponent: fileName];

if ([FileManager fileexistsatpath:p Ath])

{

nsdictionary *fileattributes = [FileManager attributesofitematpath:p ath error: Nil];

FileSize + = (double) ([FileAttributes fileSize]);

}

}

return fileSize;

}

IOS get local cache file size and erase

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.