Transferred from: http://www.cnblogs.com/taintain1984/archive/2013/03/19/2969201.html
The sandbox mechanism in iOS is a security system that specifies that an application can only read files within a folder created for the app and cannot access content elsewhere. All non-code files are saved in this place, compared to slices, sounds, attribute lists and text files.
1. Each application is within its own sandbox
2. You can't go across your sandbox to access the contents of another application sandbox
3. Applications requesting or receiving data outside the application require permission authentication
How do I see where the emulator's sandbox folder is stored on my Mac computer?
First of all, this folder is hidden, so to display these files first, open the command line:
command to display Mac hidden files: Defaults write Com.apple.finder appleshowallfiles-bool true
command to hide Mac hidden files: Defaults write Com.apple.finder Appleshowallfiles-bool false
Then restart the Finder to see the hidden files.
Then press into the appropriate folder, you can go to the emulator's sandbox file directory:
Another simple way is to directly click the Finder icon right--go to the folder--enter/users/your username/library/application support/iphone simulator/, and then confirm it. Your username is the user name of your native computer.
And then into a simulator version, and here I am 5.1
Then you can see that the applications is stored under the simulator installed in the development of the application, casually into one can see, a sandbox contains four parts:
This is the. app file, which is a running application file, documents, Apple recommends that the files created in the program or browse to the program in the file data stored in the directory, itunes backup and recovery will include this directory; library, the default settings or other status information of the storage program; library /caches: Store cached files, itunes does not back up this directory, files in this directory will not be deleted in the app exit, TMP, where temporary files are created and stored.
These directories are obtained from the following code:
1 //Get root directory2NSString *homepath =nshomedirectory (); 3NSLog (@"Home directory:%@", HomePath); 4 5 //Get the Documents folder directory, the first parameter is the description get doucments folder directory, the second parameter description is obtained in the current application sandbox, all application sandbox directory composed of data stored in an array structure6Nsarray *docpath =nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES); 7NSString *documentspath = [DocPath objectatindex:0]; 8NSLog (@"Documents directory:%@", Documentspath); 9 Ten //get the Cache directory OneNsarray *cacpath =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); ANSString *cachepath = [Cacpath objectatindex:0]; -NSLog (@"Cache directory:%@", CachePath); - the //Library Directory -Nsarray *libspath =nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); -NSString *libpath = [Libspath objectatindex:0]; -NSLog (@"Library Catalog:%@", LibPath); + - //Temp directory +NSString *temppath =nstemporarydirectory (); ANSLog (@"Temp directory:%@", TempPath);
The output results are as follows:
2012-08-03 11:10:24.325 sandboxtest[12549:f803] Home directory:/users/ryan/library/application Support/iPhone Simulator/5.1 /applications/a6b99e5a-e2c7-46e9-867a-4e7619f0da45
2012-08-03 11:10:24.325 sandboxtest[12549:f803] Documents directory:/users/ryan/library/application Support/iPhone Simulator/5.1/applications/a6b99e5a-e2c7-46e9-867a-4e7619f0da45/documents
2012-08-03 11:10:24.326 sandboxtest[12549:f803] Cache directory:/users/ryan/library/application Support/iPhone Simulator/ 5.1/applications/a6b99e5a-e2c7-46e9-867a-4e7619f0da45/library/caches
2012-08-03 11:10:24.326 sandboxtest[12549:f803] Library directory:/users/ryan/library/application Support/iPhone Simulator/ 5.1/applications/a6b99e5a-e2c7-46e9-867a-4e7619f0da45/library
2012-08-03 11:10:24.326 sandboxtest[12549:f803] Temp directory:/var/folders/7z/1wj5h8zx7b59c02pxmpynd500000gn/t/
The following begins to create a file in the directory and then writes the contents to the file:
1Nsarray *docpath =nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES); 2NSString *documentspath = [DocPath objectatindex:0]; 3 //Write File4 if(!Documentspath) { 5NSLog (@"Directory not found"); 6}Else { 7NSString *filepaht = [Documentspath stringbyappendingpathcomponent:@"Test.txt"]; 8Nsarray *array = [Nsarray arraywithobjects:@"Title",@"Contents", nil]; 9 [Array writetofile:filepaht atomically:yes]; Ten}
After the creation is successful, open the folder directory and you can see the Test.txt file:
The next step is to read the contents of the file:
1 //Read File2Nsarray *docpath =nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES); 3NSString *documentspath = [DocPath objectatindex:0]; 4NSString *readpath = [Documentspath stringbyappendingpathcomponent:@"Test.txt"]; 5Nsarray *filecontent =[[Nsarrayalloc] initwithcontentsoffile:readpath]; 6NSLog (@"file content:%@", filecontent);
The output results are as follows:
2012-08-03 11:26:53.594 sandboxtest[12642:f803] File contents: (
Title,
Contents
)
iOS sandbox mechanism (sandbox)