iOS Sandbox (sandbox)

Source: Internet
Author: User

The sandbox mechanism (sandbox) 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, such as 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

To view the storage location of the emulator's sandbox folder on your 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, tap the apple logo in the upper left corner of the screen-force quit-Select Finder and click Restart, this time you can reopen the Finder to see the hidden files.

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 your native user name

Then press into the appropriate folder, you can go to the emulator's sandbox file directory:

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, default settings for stored programs, or other status information ; 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 directory 2 NSString *homepath = Nshomedirectory ();   3 NSLog (@ "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 applications sandbox directory composed of an array structure of data stored 6 NS   Array *docpath = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);   7 NSString *documentspath = [DocPath objectatindex:0];   8 NSLog (@ "Documents directory:%@", Documentspath); 9 10//Get cache directory one by one nsarray *cacpath = Nssearchpathfordirectoriesindomains (Nscachesdirectory, N  Suserdomainmask, YES);  NSString *cachepath = [Cacpath objectatindex:0];  NSLog (@ "Cache directory:%@", CachePath); //library Catalog Nsarray *libspath = Nssearchpathfordirectoriesindomains (nslibrarydirectory  , Nsuserdomainmask, YES);  NSString *libpath = [Libspath objectatindex:0];  NSLog (@ "Library directory:%@", LibPath); //temp Catalog NSString *temppath = Nstemporarydirectory ();   NSLog (@ "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:

1     Nsarray *docpath = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);   2         nsstring *documentspath = [DocPath objectatindex:0];   3         //write file   4         if (!documentspath) {   5             NSLog (@ "directory not Found");   6         }else {   7             nsstring *filepaht = [Documentspath stringbyappendingpathcomponent:@ "test.txt"];   8             Nsarray *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 file  2     nsarray *docpath = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);  3         nsstring *documentspath = [DocPath objectatindex:0];  4         nsstring *readpath = [Documentspath stringbyappendingpathcomponent:@ "test.txt"];  5         Nsarray *filecontent = [[Nsarrayalloc] initwithcontentsoffile:readpath];  6         NSLog (@ "File contents:%@", filecontent);  

The output results are as follows:

2012-08-03 11:26:53.594 sandboxtest[12642:f803] File contents: (

Title,

Contents

)

iOS Sandbox (sandbox)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.