First, what is a sandbox
iOS apps can only read files in the file system created for the program, not anywhere else, and this area becomes a sandbox
Second, save the content
All non-code files are stored in this, examples like, icons, sounds, images, attribute lists, text files, etc.
Third, the role
iOS sandbox provides good security for running programs
Iv. Catalogue
1. Documents directory: This directory is used to store user data or other information that should be regularly backed up, Apple recommends that the files established in the program or browsed in the program are stored in this directory, itunes backup and recovery will include this directory.
2. Appname.app directory: This is the application's package directory, which contains the application itself. Because the application must be signed, you cannot modify the contents of this directory at run time, which may cause the application to fail to start.
3. Library Directory: There are two sub-directories under this directory: Caches and Preferences
The Preferences directory contains the application's preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set your application preferences
The Caches directory is used to store the application-specific support files, saving the information that is needed during the application restart.
4. tmp directory: This directory is used to store temporary files, save the application to restart the process of unnecessary information, restart and empty
Itues and iphone when syncing, back up all document and library files
How to obtain different directories
1. Get the document Directory
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentsdirectory = [Paths objectatindex:0];
2. Get the Cache directory
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
nsstring* cachesdirectory = [Paths objectatindex:0];
3. How to get the TMP directory path:
NSString *tmpdir = Nstemporarydirectory ();
4. How to obtain the path of the resource file in the application package:
For example, get a picture resource (apple.png) path in the package by:
NSString *imagepath = [[nsbundlemainbundle]pathforresource:@ "Apple" oftype:@ "PNG"];
VI. File I/O operations
1. Write the data to the documents directory:
-(BOOL) Writeapplicationdata: (nsdata*) Data tofile: (nsstring*) FileName {
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *docdir = [Paths objectatindex:0];
if (!docdir) {
NSLog (@ "Documents directory not found!");
return NO;
}
NSString *filepath = [Docdir stringbyappendingpathcomponent:filename];
return [data Writetofile:filepath Atomically:yes];
}
2. Read data from the documents directory:
-(NSData *) Applicationdatafromfile: (NSString *) FileName {
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *docdir = [Paths objectatindex:0];
NSString *filepath = [Docdir stringbyappendingpathcomponent:filename];
NSData *data = [[[NSData alloc]initwithcontentsoffile:filepath]autorelease];
return data;
}