By default, each sandbox contains 3 folders: Documents, Library, and TMP. Documents: Apple recommends that the file data that is created in the program or browsed in the program be kept in that directory;
Library: The default settings or other status information of the stored program;
TMP: Provides a place to create temporary files on the fly.
itunes backs up all documents and library files while syncing with the iphone.
When the iphone restarts, all of the TMP files are discarded.
Ways to obtain the documents path:
-(NSString *) Documentfolder {
return [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];
}
Get the path to a file in documents
NSString *path = [[Self Documentfolder] stringbyappendingpathcomponent:@ "Image.png"];
Get the TMP directory
NSString *temppath = Nstemporarydirectory ();
Add: Get the path to the application package (that is, the bundle)
-(NSString *) Bundlefolder {
return [[NSBundle Mainbundle]bundlepath];
}
First, sand box (sandbox)
For security purposes, applications can only write their own data and preferences to a few specific locations. When an application is installed on a device, a home directory is created for it, which is the sandbox for the application.
There are a total of four subdirectories in the home directory:
Documents directory: You should write all the application data files to this directory. This directory is used to store user data or other information that should be backed up regularly.
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.
Library directory: There are two subdirectories 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.
TMP directory: This directory is used to store temporary files, saving information that is not needed during the application restart.
Ways to get these directory paths:
1, get the Home directory path function:
NSString *homedir = Nshomedirectory ();
2. Method for obtaining the Documents directory path:
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *docdir = [Paths objectatindex:0];
3, get the Caches directory path method:
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory,nsuserdomainmask,yes);
NSString *cachesdir = [Paths objectatindex:0];
4. Method to get the TMP directory path:
NSString *tmpdir = Nstemporarydirectory ();
5, gets the method of the resource file path in the application package:
For example, get a picture resource (apple.png) path in the package by:
NSString *imagepath = [[NSBundle mainbundle]pathforresource:@ "Apple" oftype:@ "PNG"];
UIImage *appleimage = [[UIImage Alloc]initwithcontentsoffile:imagepath];
The Mainbundle class method in the code is used to return an object that represents the application package.
Ii.. File io
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];
return data;
}
IPhone Sandbox path