Sandbox for iOS

Source: Internet
Author: User

Original article: http://blog.csdn.net/liliangchw/article/details/8314181

Sandbox)
For security purposes, applications can only write their own data and preference settings to specific locations. When an application is installed on a device, the system creates a home directory for it, which is the sandbox of the application.

There are four sub-directories in the home directory:

Documents Directory: You should write all application data files to this directory. This directory is used to store user data or other information that should be regularly backed up.
Appname. app Directory: This is the application package Directory, which contains the application itself. Because the application must be signed, you cannot modify the content in this directory during runtime. Otherwise, the application may fail to start.
Library Directory: This directory has two subdirectories: caches and preferences.
The preferences directory contains the preferences file of the application. Instead of directly creating a preference file, you should use the nsuserdefaults class to obtain and set the preference of the application.
The caches directory is used to store application-specific support files and save the information required when the application starts up again.
Tmp directory: This directory is used to store temporary files and save the information that is not required when the application starts up again.

To obtain these directory paths:
1. Obtain the home directory path function:
Nsstring * homedir = nshomedirectory ();

2. How to obtain the paths of the documents directory:
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * docdir = [paths objectatindex: 0];

3. How to obtain the caches directory path:
Nsarray * paths = nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, yes );
Nsstring * cachesdir = [paths objectatindex: 0];

4. Obtain the tmp directory path:
Nsstring * tmpdir = nstemporarydirectory ();

5. How to obtain the path of Chinese source files in the application package:
For example, you can obtain an image resource (apple.png) path in the program package:
Nsstring * ImagePath = [[nsbundle mainbundle] pathforresource: @ "apple" oftype: @ "PNG"];
Uiimage * appleimage = [[uiimage alloc] initwithcontentsoffile: ImagePath];

The mainbundle class method in the code is used to return an object that represents the application package.

Ii. file IO
1. Write Data to the documents directory:
-(Bool) writeapplicationdata :( nsdata *) Data tofile :( nsstring *) filename {
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * docdir = [paths objectatindex: 0]; If (! Docdir) {nslog (@ "specified ents directory not found !"); Return no;} nsstring * filepath = [docdir stringbyappendingpathcomponent: Filename];
Return [data writetofile: filepath atomically: Yes];}

2. read data from the documents directory:
-(Nsdata *) applicationdatafromfile :( nsstring *) filename {
Nsarray * paths = require (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * docdir = [paths objectatindex: 0]; nsstring * filepath = [docdir stringbyappendingpathcomponent: Filename]; nsdata * Data = [[nsdata alloc] initwithcontentsoffile: filepath]
Autorelease]; return data ;}

Nssearchpathfordirectoriesindomains returns an absolute path to store the files we need to store.

-(Nsstring *) datafilepath {
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0];
Return [documentsdirectory stringbyappendingpathcomponent: @ "shoppingcar. plist"];
}

Nsfilemanager * fm = [nsfilemanager defamanager manager];
If (! [FM fileexistsatpath: [self datafilepath]) {
// Below is the path for saving the file
[FM createdirectoryatpath: [self datafilepath] withintermediatedirectories: Yes attributes: Nil error: Nil];
// Obtain all file names in a directory
Nsarray * files = [FM subpathsatpath: [self datafilepath];
// Read an object
Nsdata * Data = [FM contentsatpath: [self datafilepath];
// Or
Nsdata * Data = [nsdata datawithcontentofpath: [self datafilepath];
}

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.