IOS sandbox and clear cache, ios sandbox Cache

Source: Internet
Author: User

IOS sandbox and clear cache, ios sandbox Cache

SandBox is a security system. Each application we develop has a corresponding sandbox folder on the device. The current program can only read files in its sandbox folder, you cannot access sandbox of other applications. All non-code resources added to the project, such as video, sound, and attribute lists, are included in their sandbox. In addition, data dynamically generated during the program running or obtained from the network is also stored in the sandbox if it is to be stored.

Default Folder in sandbox

(1) Documents: Apple recommends that you store the file data created or browsed in the program in this directory. This directory will be included during iTunes backup and recovery.

(2) Library: the default setting of the storage program or other status information;

It contains two folders: Caches and Preference;

Caches: stores cached files. iTunes does not back up this directory.

(3) tmp: provides a place to instantly create temporary files

Obtain different directories in the sandbox

Code

// JRSandBoxPath. h // Fmdb // Created by jerei on 15-10-30. // Copyright (c) 2015 jerei. all rights reserved. // # import <Foundation/Foundation. h> @ interface JRSandBoxPath: NSObject // get the file directory of the sandbox Document + (NSString *) getDocumentDirectory; // get the file directory of the sandbox Library + (NSString *) getLibraryDirectory; // get the file directory of the sandbox Library/Caches + (NSString *) getCachesDirectory; // get the file directory of the sandbox Preference + (NSString *) getPreferencePanesDirectory; // get the folder directory + (NSString *) getTmpDirectory of the tmp sandbox; @ end // JRSandBoxPath. m // Fmdb // Created by jerei on 15-10-30. // Copyright (c) 2015 jerei. all rights reserved. // # import "JRSandBoxPath. h "@ implementation JRSandBoxPath # pragma mark-get the directory of the sandbox Document + (NSString *) getDocumentDirectory {return [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];} # pragma mark-get the file directory of the sandbox Library + (NSString *) getLibraryDirectory {return [NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES) lastObject];} # pragma mark-get the file directory of the sandbox Library/Caches + (NSString *) getCachesDirectory {return [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject];} # pragma mark-get the Preference directory of the sandbox + (NSString *) getPreferencePanesDirectory {return [NSSearchPathForDirectoriesInDomains (NSPreferencePanesDirectory, NSUserDomainMask, YES) lastObject];} # pragma mark-get the directory of tmp in the sandbox + (NSString *) getTmpDirectory {return NSTemporaryDirectory () ;}@ end

Clear Cache

Useful data is cached during development. When this data is not needed, it can be cleared. Here we have sorted out several methods to calculate the price, clear the specified file, and clear all files in the specified directory.

Code

// JRCleanCaches. h // Fmdb // Created by jerei on 15-10-30. // Copyright (c) 2015 jerei. all rights reserved. // # import <Foundation/Foundation. h> @ interface JRCleanCaches: NSObject // return the directory or file size according to the path + (double) sizeWithFilePath :( NSString *) path; // get all files under the specified directory + (NSArray *) getAllFileNames :( NSString *) dirPath; // Delete the specified directory or file + (BOOL) javasacheswithfilepath :( NSString *) path; // clear the files in the specified directory + (BOOL) Export achesfromd IrectoryPath :( NSString *) dirPath; @ end /// JRCleanCaches. m // Fmdb // Created by jerei on 15-10-30. // Copyright (c) 2015 jerei. all rights reserved. // # import "JRCleanCaches. h "@ implementation JRCleanCaches # pragma mark-return directory or file size based on path + (double) sizeWithFilePath :( NSString *) path {// 1. obtain the Folder Manager NSFileManager * manger = [NSFileManager defaultManager]; // 2. check path rationality BOOL dir = NO; BOOL exits = [manger fi LeExistsAtPath: path isDirectory: & dir]; if (! Exits) return 0; // 3. determine whether it is a folder if (dir) {// folder, traverse all files in the folder // This method can get all sub-paths under this folder (direct \ indirect Sub-paths) NSArray * subpaths = [manger subpathsAtPath: path]; int totalSize = 0; for (NSString * subpath in subpaths) {NSString * fullsubpath = [path stringByAppendingPathComponent: subpath]; BOOL dir = NO; [manger fileExistsAtPath: fullsubpath isDirectory: & dir]; if (! Dir) {// The Sub-path is a file NSDictionary * attrs = [manger attributesOfItemAtPath: fullsubpath error: nil]; totalSize + = [attrs [NSFileSize] intValue];} return totalSize/(1024*1024.0);} else {// file NSDictionary * attrs = [manger attributesOfItemAtPath: path error: nil]; return [attrs [NSFileSize] intValue]/(1024.0*1024.0) ;}# pragma mark-Get all files in the specified directory + (NSArray *) getAllFileNames :( NSString *) dirPath {NS Array * files = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath: dirPath error: nil]; return files ;}# pragma mark-delete a specified directory or file + (BOOL) Export acheswithfilepath :( NSString *) path {NSFileManager * mgr = [NSFileManager defaultManager]; return [mgr removeItemAtPath: path error: nil] ;}# pragma mark-clear files in the specified directory + (BOOL) export achesfromdirectorypath :( NSString *) dirPath {// get all file arrays NSArray * fileAry = [JRClean Caches getAllFileNames: dirPath]; // traverses the array BOOL flag = NO; for (NSString * fileName in fileAry) {NSString * filePath = [dirPath stringByAppendingPathComponent: fileName]; flag = [JRCleanCaches ‑acheswithfilepath: filePath]; if (! Flag) break;} return flag;} @ end

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

Related Article

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.