IOS Clear Cache

Source: Internet
Author: User

The iOS application can only read files in the file system created for the program, and it cannot be accessed anywhere else, and this area becomes a sandbox . It consists of three folders:

Documents : Apple recommends that file data that is established in the program or browsed in the program be kept in this directory, and that itunes backup and restore will include this directory, such as user information, such as permanent files;

Library: It contains two folders caches and preferences
library/caches: store cache files, itunes does not back up this directory, files in this directory will not be deleted in the app exit, slice, video cache;
library/preferences: contains the application's preferences file;

Temp: This directory is used to store temporary files, saving information that is not needed during the application restart.
Here's how to get the sandbox path:

1,//The root directory of the sandbox nsstring *homepath = Nshomedirectory ();

2,///Sandbox documents path nsstring *docupath = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES) lastobject];

3,//sandbox in the library path nsstring *libpath = [Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask , YES) lastobject];

< Span class= "hljs-literal" >< Span class= "Hljs-comment" > 4,//sandbox library/caches path nsstring *cachespath = [ Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) Lastobject];

5,///Sandbox temp path nsstring *temppath = Nstemporarydirectory ();

Calculate the size of a folder

-(float) Calculatefoldersizewithpath: (NSString *) path{

Nsfilemanager *filemanager=[nsfilemanager Defaultmanager];

NSString *cachepath=[nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) firstObject];

Cachepath=[cachepath Stringbyappendingpathcomponent:path];

CGFloat foldersize=0;

if ([FileManager Fileexistsatpath:cachepath])

{

Nsarray *childfiles=[filemanager Subpathsatpath:cachepath];

For (NSString *filename in Childfiles)

{

NSString *fileabsolutepath=[cachepath Stringbyappendingpathcomponent:filename];

CGFloat size=[self Calculatefilesizewithpath:fileabsolutepath];

Foldersize + = size;

NSLog (@ "fileabsolutepath=%@", Fileabsolutepath);

}

Sdwebimage The framework itself calculates the cache

Foldersize+=[[sdimagecache Sharedimagecache] getsize];

return foldersize/1024.0/1024.0;

}

return 0;

}

Calculate a single File size

-(CGFloat) Calculatefilesizewithpath: (NSString *) path{

Nsfilemanager *filemanager=[nsfilemanager Defaultmanager];

if ([FileManager Fileexistsatpath:path]) {

CGFloat Size=[filemanager Attributesofitematpath:path error:nil].filesize;

return size;

}

return 0;

}

-(void) Clearcacheclick

{

Nsarray *arr_path = @[@ "/media", @ "/music", @ "/voice"];

[[UIView alloc]showhudwithtitle:@ is clearing the cache ...] WITHSTATE:1];

One is to clear the sdwebimage cache, and the second is to clear the custom file cache

[[Sdimagecache Sharedimagecache] cleardiskoncompletion:^{

Dispatch_async (dispatch_get_global_queue (0, 0), ^{

NSString *documentdir = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) OBJECTATINDEX:0];

For (NSString *pathname in Arr_path) {

NSString *fileabsolutepath = [Documentdir stringbyappendingpathcomponent:pathname];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

if ([FileManager Fileexistsatpath:fileabsolutepath]) {

[FileManager Removeitematpath:fileabsolutepath Error:nil];

[FileManager Createdirectoryatpath:fileabsolutepath withintermediatedirectories:yes Attributes:nil Error:nil];

}

}

Dispatch_async (Dispatch_get_main_queue (), ^{

[[UIView alloc]showandhidehudwithtitle:@] Clear complete "withstate:0";

Set text

Self.lable_Cache.text = @ "0.00 MB";

});

});

}];

}

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.