[Understanding] iOS sandbox mechanism (I)

Source: Internet
Author: User

[Understanding] iOS sandbox mechanism (I)
1. IOS sandbox Mechanism

IOS applications can only read files from the file system created for the modified program. They cannot be accessed from other places. This area is a sandbox, therefore, all non-code files must be stored here, such as icons, sounds, images, attribute lists, and text files.

1.1. Each application has its own storage space.
1.2. applications cannot access the content of other buckets through their own walls.
1.3. The data requested by the application must pass permission detection. If the data does not meet the conditions, it will not be released.
Through this figure, we can only understand from the surface that sandbox is a security system. All operations of an application must be performed through this system. The core content of this system is: sandbox has the permission to perform various operations on the application.

2. Open the sandbox directory of the simulator.

Next let's take a look at the location of the sandbox folder of the simulator on mac.

Files are stored in a hidden folder under the personal username folder. The Chinese name is the resource Library. Its Directory is actually the Library.

2.1 Method 1: You can set to show hidden files and open them directly under the Finder. To view hidden files, open the terminal and enter a name.

 

Command for displaying Mac hidden files: defaults write com. apple. finder AppleShowAllFiles-bool true

Command for hiding Mac hidden files: defaults write com. apple. finder AppleShowAllFiles-bool false

After entering the key, click Enter to exit the terminal. Restart the Finder and restart the Finder. Click the apple icon in the upper left corner of the window --> force exit --> Finder -->

Now you can see the resource library folder.

Open the resource library and find the/Application Support/iPhone Simulator/folder. This is the sandbox directory of each program in the simulator.

2.2 Method 2. This method is more convenient. On the Finder, click-> go to the folder and enter/Users/username/Library/Application Support/iPhone Simulator.

Enter your username here.

3. directory structure

By default, each sandbox contains three folders: Documents, Library, and tmp. Because of the sandbox mechanism of the application, the application can only read and write files under several directories.
Documents: Apple recommends that you store the file data created or browsed in the program in this directory. This directory will be included during iTunes backup and recovery.
Library: the default setting of the storage program or other status information;

Library/Caches: stores cached files. iTunes will not back up this directory, and files under this directory will not be deleted after the application exits.

Tmp: provides a place to create temporary files in real time.

 

During synchronization with the iPhone, iTunes backs up all Documents and Library files.

When the iPhone restarts, It discards all tmp files.

 

We create an IosSandbox project to perform operations such as sandbox and file read/write.

After creation, find the corresponding directory on the simulator,

The directory is fully expanded.

These are the three directories mentioned above: Documents, Library, and tmp.

 

1. Get the Home Directory of the program

NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * path = [pathsobjectAtIndex: 0]; NSLog (@ "path: % @", path); print the result:
[Cpp] view plaincopy 2012-06-1714: 00: 06.098 IosSandbox [3536: f803]/Users/rongfzh/Library/ApplicationSupport/iPhoneSimulator/5.1/Applications/templates

 

What about the directory on the real machine? Let's take a look

14:25:47. 059 IosSandbox [4281: f803]/var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2

The directory on the real machine is/var/mobile/Applications/, which is different from that on the simulator. This is the Home directory. Other subdirectories are the same as those in the simulator.

 

2. Obtain the document directory NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * path = [pathsobjectAtIndex: 0]; NSLog (@ "path: % @", path); print the result 2012-06-1714: 00: 06.099 IosSandbox [3536: f803] path: /Users/rongfzh/Library/ApplicationSupport/iPhoneSimulator/5.1/Applications/Documents 3. Obtain the Cache directory NSArray * paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES ); NSString * path = [pathsobjectAtIndex: 0]; NSLog (@ "% @", path); print the result
2012-06-1714: 03: 50.431 IosSandbox [3628: f803]/Users/rongfzh/Library/ApplicationSupport/iPhoneSimulator/5.1/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Caches
4. Obtain the Library directory NSArray * paths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES); NSString * path = [pathsobjectAtIndex: 0]; NSLog (@ "% @", path ); print the result 2012-06-1714: 07: 17.544 IosSandbox [3733: f803]/Users/rongfzh/Library/ApplicationSupport/iPhoneSimulator/5.1/Applications/libraries/Library 5. Obtain the Tmp directory NSString * tmpDir = NSTemporaryDirectory (); NSLog (@ "% @", tmpDir ); Print the result 2012-06-1714: 08: 07.824 IosSandbox [3782: f803]/var/folders/g7/logs/T/6. Write the file NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * docDir = [pathsobjectAtIndex: 0]; if (! DocDir) {NSLog (@ "events directory not found");} NSArray * array = [[NSArrayalloc] initWithObjects: @ "content", @ "content", nil]; NSString * filePath = [docDirstringByAppendingPathComponent: @ "testFile.txt"]; [arraywriteToFile: filePathatomically: YES];

Note: We also run it on the real machine to write the file and read the content from the real machine.

Write the input array, which contains two strings. We will read and print them later.

Write the file testFile.txt in the sandbox directory of the program.

Open the file and you will see the following content: A plist file in xml format that stores the content.

7. Read the file NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * docDir = [pathsobjectAtIndex: 0]; NSString * filePath = [paths: @ "testFile.txt"]; NSArray * array = [[NSArrayalloc] initWithContentsOfFile: filePath]; NSLog (@ "% @", array); print the result:

After the above file is parsed, the content is printed out.

2012-06-1714: 14: 46.249 IosSandbox [3918: f803] ("\ U5185 \ U5bb9", content)

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.