iOS sandbox mechanism

Source: Internet
Author: User

First, what is a sandbox

iOS apps can only read files in the file system created for the program, not anywhere else, and this area becomes a sandbox
Second, save the content
All non-code files are stored in this, examples like, icons, sounds, images, attribute lists, text files, etc.
Third, the role
iOS sandbox provides good security for running programs
Iv. Catalogue
1. Documents directory: This directory is used to store user data or other information that should be regularly backed up, Apple recommends that the files established in the program or browsed in the program are stored in this directory, itunes backup and recovery will include this directory.
2. Appname.app directory: This is the application's package directory, which contains the application itself. Because the application must be signed, you cannot modify the contents of this directory at run time, which may cause the application to fail to start.
3. Library Directory: There are two sub-directories under this directory: Caches and Preferences
The Preferences directory contains the application's preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set your application preferences
The Caches directory is used to store the application-specific support files, saving the information that is needed during the application restart.
4. tmp directory: This directory is used to store temporary files, save the application to restart the process of unnecessary information, restart and empty
Itues and iphone when syncing, back up all document and library files
How to obtain different directories
1. Get the document Directory
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentsdirectory = [Paths objectatindex:0];
2. Get the Cache directory
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
nsstring* cachesdirectory = [Paths objectatindex:0];
3. How to get the TMP directory path:
NSString *tmpdir = Nstemporarydirectory ();
4. How to obtain the path of the resource file in the application package:
For example, get a picture resource (apple.png) path in the package by:
NSString *imagepath = [[nsbundlemainbundle]pathforresource:@ "Apple" oftype:@ "PNG"];
VI. File I/O operations
1. Write the 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 (@ "Documents 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 = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);
NSString *docdir = [Paths objectatindex:0];
NSString *filepath = [Docdir stringbyappendingpathcomponent:filename];
NSData *data = [[[NSData alloc]initwithcontentsoffile:filepath]autorelease];
return data;
}



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.