IOS Sandbox (sandbox) mechanism and file operations

Source: Internet
Author: User

1. iOS sandbox mechanism (Sandbox (sandbox) mechanism)

iOS applications can only read files in the file system created for the program, not access to other places, this area is a sandbox, so all non-code files to be stored in this, such as icons, sounds, images, attributes list, text files and so on.

1.1. Each application has its own storage space
1.2, the application can not turn over their walls to access the contents of other storage space
1.3, the application requests the data to pass the permission detection, if does not meet the condition, will not be released.
This diagram can only be understood from the surface of the sandbox is a security system, all the operations of the application through the system to execute, the core content is: Sandbox the application to perform various operations of the permission restrictions.


--------xode6 The following access methods are referenced below:----------

2. Open the emulator sandbox directory

Here's a look at where the emulator's sandbox folder is on your Mac computer.

The files are in a hidden folder under the personal User name folder, the Chinese is called the Repository, his directory is actually the library.

2.1 Method 1, you can set the display hidden files, and then open directly under the Finder. Here's how to set up viewing hidden files: Open terminal, enter name

  

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

After you have finished, click Enter, exit the terminal, restart the Finder to reboot the finder: mouse click the Apple icon in the upper-left corner of the window and force exit-->finder-->

You can now see the repository folder.

Locate the/application Support/iphone simulator/folder after opening the repository. This is the sandbox directory of the simulator's various programs.


2.2 Method 2, This method is more convenient, on the Finder point-to go to the folder, enter the/users/username/library/application support/iphone simulator/to.

Username here to write your user name.

3. Directory structure

By default, each sandbox contains 3 folders: Documents, Library, and TMP.
Documents: Apple recommends that the file data that is created in the program or browsed in the program be kept in that directory;
Library: The default settings or other status information of the stored program;
TMP: Provides a place to create temporary files on the fly.

itunes backs up all documents and library files while syncing with the iphone.

When the iphone restarts, all of the TMP files are discarded.




-----------xcode6 The above version refer to the following------------------------------------


By default, each sandbox contains 3 folders: Documents, Library, and TMP. Applications can only read and write files in several directories because of the sandboxed mechanism applied
Documents: Apple recommends that file data that is created in the program or browsed in the program be saved in this directory, which is included in itunes backup and restore
Library: The default settings or other status information of the stored 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: Provides a place to create temporary files on the fly.


itunes backs up all documents and library files while syncing with the iphone.
When the iphone restarts, all of the TMP files are discarded.

The reference code can be used:

// get full path name

nsstring *filename = [Plistpathstringbyappendingpathcomponent:@ "Textdemo.plist"];

NSLog (@ " works under filename is%@", filename);



1. Create Project Plistdemo


After the project is created, you can find the corresponding plist file for the project, which opens as shown:


In the editor to display similar to the form of a table, you can right-click on the plist, open with source code, you can see the XML format of the plist file.



2. Create a plist file.


Press Command +n shortcut key creation, or file-> new-> New File, select the property List under Mac OS X



Create the plist file named Plistdemo.



Open the Plistdemo file, right-click in the blank, right-click Add Row, and add data, after adding a successful data, right-click on this data to see value Type select Dictionary. Click the plus sign to add the data under this dictionary



After creation, use source code to see what the plist file looks like:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE plist Public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd" ><plist Version= "1.0" ><dict><key> Guangdong </key><array><string> zhaoqing </string><string > shenzhen </string><string> guangzhou </string></array><key> Sichuan </key><array>< string> nanchong </string><string> Mianyang </string><string> Chengdu </string></array></ Dict></plist>

3. Read the data of plist file and print reference code as follows:

NSBundle *bundle = [NSBundle mainbundle];        NSString *path = [Bundle pathforresource:@ "Citys" oftype:@ "plist"];        Read the data into the Nsdictionary dictionary nsdictionary *dictionary = [[Nsdictionary Alloc]initwithcontentsoffile:path];                NSLog (@ "size is%d", [dictionary Count]);        Nsarray *array = [dictionary AllKeys];            The loop output dictionary has data for the (ID key in dictionary) {Nsarray *arrays = [dictionary Objectforkey:key];            for (int i =0; i < [arrays count]; i++) {NSLog (@ "key:%d, value:%@", I,[arrays objectatindex:i]);        }}//plist file has write operation nsmutabledictionary *data = [[Nsmutabledictionary alloc]init];        Add data [setobject:@ "one" forkey:@ "1"];        [Data setobject:@ "forkey:@" 2 "];        [Data setobject:@ "There" forkey:@ "3"];        [Data setobject:@ "four" forkey:@ "4"]; Write data into sandbox nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NsusErdomainmask, YES);        NSString *plistpath = [Paths objectatindex:0];        Get the full path name nsstring *filename = [Plistpath stringbyappendingpathcomponent:@ "Textdemo.plist"];        NSLog (@ "works under filename is%@", filename);                Create and write to file [data writetofile:filename Atomically:yes];        Check if written as nsmutabledictionary *writedata = [[Nsmutabledictionary alloc]initwithcontentsoffile:filename]; NSLog (@ "Write Data is%@", writedata);


Of course the console has been printed out, huh?


For file operation, first write here, more complex nsdata or SQLite save, later use, learn to add it.


IOS Sandbox (sandbox) mechanism and file operations

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.