Document folder:
ITunes automatically backs up the directory for persistent data that should be generated by the program's runtime. (Apple recommends that programs created and browsed in the program be stored here, and iTunes automatically includes the directory when backing up and replying.)
Acquisition methods:
// The file path is an array, where the first element is taken
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
Library folder:
iTunes also backs up the directory automatically for storing default settings and other status information for the program.
Acquisition methods:
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
Library/Caches:
To store cached files, iTunes does not backup this directory. Files in this directory will not be deleted after the program exits. Generally, large but not very important files are stored.
Acquisition methods:
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
Library/Preferences:
Used to store user preferences, iOS settings will find application settings in this directory, iTunes will automatically backup the directory, usually this folder is maintained by the system, it is recommended not to operate him.
The system does not have a way to get the folder path directly. It needs to get the Library path first and then do string splicing to find the path.
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDoMainMask, YES)[0];
NSString*preferencePath = [libraryPath stringByAppendingString:@"/Preferences"];
tmp:
Save the temporary folder of the application and delete the corresponding files from the directory after using it. If space is insufficient, the system may delete the files in the directory. iTunes will not synchronize the folder. When the iPhone restarts, the files in the directory will be deleted.
The method of obtaining this path is different from other methods. It has its own method:
NSString *tmpPath = NSTemporaryDirectory();
*/
Create a new tool class ClearCacheTool
#import <Foundation/Foundation.h> @interface ClearCacheTool : NSObject /*s* * Get the size of the folder under the path path path * * @param path The folder path to get * * @return Size of folders under path path return */ + (NSString *)getCacheSizeWithFilePath:(NSString *)path; /** * Clear the cache of folders under path path path * * @param path To clear the cached folder path * * @return Whether Clearance is Successful */ + (BOOL)clearCacheWithFilePath:(NSString *)path; @end
#import "ClearCacheTool.h" @implementation ClearCacheTool #pragma mark - Gets folder size under path path path + (NSString *)getCacheSizeWithFilePath:(NSString *)path{ // Get all files under the "path" folder NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path]; NSString *filePath = nil; NSInteger totleSize = 0; for (NSString *subPath in subPathArr){ // 1. Full Path of Stitching Each Document filePath =[path stringByAppendingPathComponent:subPath]; // 2. Is it a folder, not by default? BOOL isDirectory = NO; // 3. Judging whether a document exists BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory]; // 4. The purpose of the above judgment is to ignore files that do not require calculation. if (!isExist || isDirectory || [filePath containsString:@".DS"]){ // Filtration: 1. Folder does not exist 2. Filtration folder 3. Hidden files continue; } // 5. Specify the path to get the attributes of the path NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; /** attributesOfItemAtPath: Folder Path This method can only get the attributes of files, but can not get the attributes of folders, so it is also the reason why we need to traverse every file of folders. */ // 6. Get the size of each file NSInteger size = [dict[@"NSFileSize"] integerValue]; // 7. Calculate the total size totleSize += size; } //8. Convert folder size to M/KB/B NSString *totleStr = nil; if (totleSize > 1000 * 1000){ totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f]; }else if (totleSize > 1000){ totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ]; }else{ totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f]; } return totleStr; } #pragma mark - Clear cache size under path folder + (BOOL)clearCacheWithFilePath:(NSString *)path{ //Subfolders to the next directory of the path path path NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; NSString *filePath = nil; NSError *error = nil; for (NSString *subPath in subPathArr) { filePath = [path stringByAppendingPathComponent:subPath]; //Delete subfolders [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]; if (error) { return NO; } } return YES; } @end
Call:
NSString *tmpPath = NSTemporaryDirectory(); NSString *size = [ClearCacheTool getCacheSizeWithFilePath:tmpPath]; NSLog(@"-----%@------",size); BOOL flag = [ClearCacheTool clearCacheWithFilePath:tmpPath]; NSLog(@"%i",dog);
Clear the cache in the tmp directory as needed