http://blog.csdn.net/jinnchang/article/details/44828189
1. Introduction of sandbox mechanism
The sandbox mechanism (sandbox) in IOS is a security system.
Each IOS application has a separate file system (storage space) and can only operate on the corresponding file system, which is called a sandbox. All non-code files are stored here, such as property file plist, text files, images, icons, media resources, and so on.
2. Sandbox directory structure
Typically, each sandbox contains the following directories and files:
- /appname.app The package directory for the application. Because the application must be signed, the content in this directory cannot be modified at run time, or the application will not start.
- /documents/save important data files and user data files for your application. The directory is backed up when ITunes syncs.
- /library/caches Save the support files and cache files that are generated when the application is used, and the log files are best placed in this directory. ITunes does not back up this directory when syncing.
- /library/preferences saves the application's preferences file (created when using the Nsuserdefaults class settings and should not be created manually).
- /tmp/saves the temporary data needed to run the app, and when the iphone restarts, it clears all files in that directory.
The directory structure looks like this:
Add 1: For the above description can be such an example of understanding, a Notepad application, the user write things need to be saved, these things are generated by the user, you need to put in the Documents directory. A news app, if you need to download something from the server to show the user, the downloaded items are placed in the Library/caches directory. The Apple audit is strict with this requirement, mainly because of the ICloud sync issue.
Supplement 2: If you want to know the real machine or simulator App sandbox path, you can get it by executing the following code in the project:
[OBJC]View PlainCopy
- NSString *homedirectorypath = Nshomedirectory ();
3. Get the directory paths in the sandbox
[OBJC]View PlainCopy
- Get the Sandbox root directory path
- NSString *homedirectorypath = Nshomedirectory ();
- Get Applications Path
- NSString *appdirectorypath = [Nssearchpathfordirectoriesindomains (nsapplicationdirectory, NSUserDomainMask, YES ) Objectatindex:0];
- Get Documents Path
- NSString *documentdirectorypath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Objectatindex:0];
- Get Caches Path
- NSString *cachesdirectorypath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, NSUserDomainMask, YES) Objectatindex:0];
- Get the TMP path
- NSString *tmpdirectorypath = Nstemporarydirectory ();
4, Nsfilemanager
You can use FileManager to manipulate directories and files in the sandbox. A single example of Nsfilemanager can be obtained by the following method:
[OBJC]View PlainCopy
- [Nsfilemanager Defaultmanager]
5. Package (NSBundle)
IOS apps are encapsulated by bundles and can be narrowly understood as appname.app files in the sandbox. In the Finder, the bundle as a file display so as to prevent user misuse caused the program file corruption, but in fact, inside is a directory, including images, media resources, compiled code, NIB files, etc., this directory is called the main bundle.
The COCAOA provides a nsbundle class that encapsulates the bundle operation.
[OBJC]View PlainCopy
- Get the main bundle of the application
- NSBundle *mainbundle = NSBundle. Mainbundle;
- Get the resource path using the main bundle
- NSString *filepath = [Mainbundle pathforresource:@ "logo" ofType:@ "PNG"];
IOS file operations: Sandbox, File Operations (FileManager), Packages (NSBundle)