One, each iOS App SDK is limited in the "sandbox", "sandbox" is equivalent to a folder with only the owner visible permissions, Apple has the following restrictions on the sandbox.
(1), the application can operate in its own sandbox, but cannot access any other application's sandbox.
(2), data cannot be shared between applications, files in the sandbox cannot be copied to other application folders, and files in other application folders cannot be copied into the sandbox.
(3), Apple prohibits any read and write files outside the sandbox, and prohibits the application from writing content to a folder other than the sandbox.
(4), there are four folders in the root directory of the sandbox:
Appname.app directory: This is the application's package directory, which contains the application itself. Because the application must be signed,
Therefore, you cannot modify the contents of this directory at run time, or it may cause the application to fail to start.
Documents directory: The application data file should generally be stored in this folder, for storing user data or other should be regularly
The backup information.
Library Directory: There are two folders under this file, caches store application to start the required information; Preferences Package
Contains the application preferences file, but do not modify the good settings here.
TMP directory: Holds temporary files, that is, the application starts the unwanted files again.
Second, get the sandbox path
(1), the method to get the sandbox root directory, there are the following:
1, using Nshomedirectory to obtain
NSString *path = Nshomedirectory ();//main directory NSLog (@ "nshomedirectory:%@ \ n", path);
2. Access by user name
NSString *username = Nsusername (); // gets the user name of the application NSString *rootpath = nshomedirectoryforuser (userName); NSLog (@ "", RootPath);
(2), Get document Path
nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); *documentsdirectory = [Paths Objectatindex:0]; // Documents Directory NSLog (@ "", documentsdirectory);
(3), get the TMP path
NSString *tempaths = Nstemporarydirectory (); // write the data in it, the program will exit without NSLog (@ "tempaths:%@ \ n", tempaths);
(4), get the cache path
Nsarray *cachespaths = nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES); NSString *cachesdirectory = [cachespaths objectatindex:0]; NSLog (@ "nscacheddirectory:%@ \ n", cachesdirectory);
Third, write the file
1 //Get document Path2Nsarray *paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);3NSString *documentsdirectory=[paths Objectatindex:0];//Documents Directory4NSLog (@"nsdocumentdirectory:%@ \ n", documentsdirectory);5 6 //writing files to Documentdirectory7Nsarray *array = [[Nsarray alloc] Initwithobjects:@"Hello",@"content", nil];8NSString *filepath = [Documentsdirectory stringbyappendingpathcomponent:@"Test.txt"];9[Array Writetofile:filepath atomically:yes];
Four, read the file
1 NSString *filepath = [documentsdirectory stringbyappendingpathcomponent:@ "test.txt" ]; 2 Nsarray *array_1 = [[Nsarray Alloc]initwithcontentsoffile:filepath]; 3 NSLog (@ " read file in document:%@", array_1);
iOS sandbox (sandbox) mechanism and get sandbox path