Easy to master the use of sandbox sandbox in IOS application development _ios

Source: Internet
Author: User

I. iOS sandbox mechanism

iOS applications can only access the area created for the application, inaccessible to other areas, the application of other non-code files exist in this directory, including pictures, attributes file plist,bundle,nib files, etc., this area is called the Sandbox (SandBox).

Each application has its own storage space, that is, the sandbox.
The application can only access its own sandbox and cannot access other areas.
If your application requires file manipulation, you must store the file in a sandbox, especially the database file, which you can access when you are working on your computer, but you must copy the database files to the sandbox if you want to install them on the real machine.
Second, open the sandbox path

1. If you do not know the sandbox path, you can print its path in your application.

Copy Code code as follows:

NSLog (@ "sandbox path:%@", nshomedirectory ());


will get the printed result * for the letter or number, that is, the path of the sandbox

Copy Code code as follows:

/users/apple/library/application Support/iphone simulator/6.1/applications/******-****-****-****-************

Open Finder, select Go-Go to Folder (or select shortcut key command+shift+g) Enter the printed path

2, open the Finder, choose to go and hold down the option key, into the resource pool

Select the application support-iphone simulator-Select the emulator version to see the sandbox directory for each application.

Select one of the directories to see the app file with the name of the program.

Third, directory structure

Sandbox by default there are three directory folders Documents,library,tmp and app files that can only be manipulated in the appropriate folders

Documents: Generally used to store files created in an application, such as database files, or file data that is browsed in a program. If you make a backup, the files in this folder will be included;
Library: Store The default settings and state information for the application;
Library/cache: Used to hold cached files, the data under this folder will not be deleted when the application exits. This folder is not included in the backup;
TMP: Temporary files for instant wear
App file with icon:

When selected, display the package content, you can see the stored icon, nib file, property list, etc.

Four, in the sandbox to write the file

Add a plist file under the Documents folder in the sandbox directory, add a picture, and so on the same method

Copy Code code as follows:

Get Documents folder Directory
NSString *rootpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) objectAtIndex : 0];

Set the plist path
[RootPath stringbyappendingpathcomponent:@ "New.plist"];

Then write the contents in the Plist
NSString *error;
Serializes a value of "OK"
ID plist = [nspropertylistserialization datafrompropertylist:@ "OK" format:nspropertylistxmlformat_v1_0 errordescription:&error];

if (plist) {
NSLog (@ "No Error creating XML data.");
[Plist Writetofile:plistpath Atomically:yes];
}
else {
NSLog (@ "%@", error);
[Error release];
}

After running, you will see a new.plist file under the Documents folder.

There is a value OK in the file, you can also add the array, dictionary and other related types of content, just the "OK" above to a (ID) type of value can be

V. Copy files to the sandbox directory

The following copy of a public.xml file to the document path, can also copy other files, just the file name and type can be, especially the database file, must be copied to the sandbox to use. This file cannot be a computer file and must be included in the bundle of the project.

Copy Code code as follows:

Get Documents Path
NSString *documentspath = [Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes) Lastobject];

Set the path and name of the file to be copied
NSString *xmlsandboxpath = [Documentspath stringbyappendingpathcomponent:@ "Public.xml"];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

To determine if a file already exists
BOOL isexisting = [FileManager Fileexistsatpath:xmlsandboxpath];

if (!isexisting) {

If this file is not local, the file is copied to the local directory.
NSString *xmlfilepath = [[NSBundle mainbundle] pathforresource:@ "public" oftype:@ "xml"];
Nserror *err;
Copy files from bundle to the sandbox directory
[FileManager Copyitematpath:xmlfilepath Topath:xmlsandboxpath error:&err];
}

After the operation, this file is available under the documents path.

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.