Research on the Structure of iOS SandBox, the structure of iossandbox

Source: Internet
Author: User

Research on the Structure of iOS SandBox, the structure of iossandbox

When you run the iOS program in the simulator, a SandBox is created for the program ). First, we declare that my system is Max OS X 10.7.3 and the compiler is Xcode 4.3.2. To find the sandbox directory, first run the Finder, and then find it in the menu bar of the Finder-go to the folder ..., Enter

/Users/UserName/Library/Application Support/iPhone Simulator/

Here, UserName refers to the user name in the Mac system.

Click "go" to go To the simulator directory:

5.0 and 5.1 are the simulators. Open 5.1 here:

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

The Applications directory is where the installation program is located. Open it:

There are two folders. The folder name is complex. These two folders can be viewed as two programs installed in the 5.1 simulator. Their names are automatically created when running in Xcode and are randomly generated globally unique identifiers. Expand the second folder here. It is generated during the running of the small example in the previous article:

As shown in, the program contains three folders: Documents, Library, and tmp.

1. Documents:

This directory is used to store data in the program, except the Setting data based on NSUserDefaults.

The code for getting the program's Documents directory is:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];

The address of the Documents directory is stored in documentsDirectory.

Ps:

The NSUserDomainMask Constant indicates that we want to search for sandboxes restricted to our applications.

2. Library:

This directory contains two subdirectories: Caches and Preferences. (There may be many other folders, such as Cookies)

Preferences stores the Settings Data Based on NSUserDefaults. For example, you can see a plist file, which is the configuration item we set last time and is read and written by the Settings program. Open it and we can see that the last setting we made was saved in this file:

2.1 The code for getting the Library directory address is similar to those of Documents:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);NSString *libraryDirectory = [paths objectAtIndex:0];

2.2 obtain the 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 deleting the content under the tmp directory.

Get the tmp directory:

The methods for obtaining the tmp directory written on many documents are as follows:

NSString *tempPath = NSTemporaryDirectory();

But what I found was not the tmp directory of the program,:

/var/folders/tp/pl9mdxtx199bs8_k29n89ff80000gn/T/

It seems that there is no such parameter as NSLibraryDirectory that can be directly used to obtain this directory.

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

// Obtain the program's root directory NSString * homeDirectory = NSHomeDirectory (); // obtain the tmp directory address NSString * tmpDirectory = [homeDirectory stringByAppendingPathComponent: @ "tmp"];

TmpDirectory stores the tmp directory of the program.

 

 

I can use FileManager to create sub-directories and files in it, but I have a program that tries to download files to it using FTP, but a file write error occurs.

 

 

After obtaining the preceding directory address, you only need:

NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"filename.txt"];

On a real machine, NSTemporaryDirectory corresponds to the tmp directory of the app directory. Thanks to @ xeonwell's guidance.

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.