Research on the structure of iOS sandbox

Source: Internet
Author: User

Running an iOS program in the emulator creates a sandbox (sandbox) for the program. First of all, the system I'm using is Max OS X 10.7.3, and the compiler is Xcode 4.3.2. To find the sandbox directory, run the Finder first, then find the go-to folder in the Finder's menu bar ... in the window that opens

/users/username/library/application Support/iphone simulator/

Where username refers to the user's name in the Mac system.

Click "Go" to the emulator directory:

5.0, 5.1 is the version of the simulator. Here Open 5.1:

The directory structure in is similar to the directory structure in the actual iOS device.

The applications directory is where the installer is installed. Open it:

You can see that there are two folders, the name of the folder is very complex. These two folders can be viewed as two programs that are now installed in the 5.1 emulator. Their names are created automatically by running in Xcode, and the names are randomly generated globally unique identifiers. This expands the second folder. It is the last article that was generated when the small example was run:

As shown, the program contains three folders: Documents, library, and TMP.

1. Documents:

This directory is used to store data in the program, except for those settings based on Nsuserdefaults.

The code to get the documents directory for the program is:

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentsdirectory = [Paths objectatindex:0];

The address of the documents directory is stored in the documentsdirectory.

Ps:

The Nsuserdomainmask constant indicates that we want to search for a sandbox that is restricted to our application.

2. Library:

This directory contains two subdirectories: caches and preferences. (There may be many other folders, such as cookies)

Preferences This directory is based on nsuserdefaults settings data, for example, you can see a plist file, which is the last time we made the setup item, is the settings program read and write. Open it and you can see that the last setting we made was saved in this file:

2.1 The code to get the library directory address is similar to documents:

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES); NSString *librarydirectory = [Paths objectatindex:0];

2.2 Get caches directory address:

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); nsstring* cachesdirectory = [Paths objectatindex:0];

3. TMP:

We can store temporary files in this directory. Our program should be responsible for removing the contents of the TMP directory.

Get the TMP directory:

I see a lot of stuff. The way to get the TMP directory is:

NSString *temppath = Nstemporarydirectory ();

But what I found was not the TMP directory for the program, but:

/var/folders/tp/pl9mdxtx199bs8_k29n89ff80000gn/t/

It seems that there is not a parameter like nslibrarydirectory that can be used to get this directory directly.

However, if we can get the root directory of this program, we can get the TMP directory:

Gets the root directory of the program NSString *homedirectory = Nshomedirectory ();//Gets the address of the TMP directory nsstring *tmpdirectory = [homedirectory stringbyappendingpathcomponent:@ "TMP"];

The TMP directory of this program is stored in the tmpdirectory.

I can do this when I use FileManager to create subdirectories and files, but I have a program that tries to download files to it using FTP, but there is a file write error.

After obtaining the above directory address, you want to obtain the files or subdirectories, as long as:

NSString *filename = [documentsdirectory stringbyappendingpathcomponent:@ "filename.txt"];

On the real machine, nstemporarydirectory corresponds to the TMP directory of the app directory. thank @xeonwell for his guidance.

Research on the structure of iOS sandbox

Related Article

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.