IOS sandbox Mechanism

Source: Internet
Author: User

IOS sandbox Mechanism

First, what is sandbox?

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.
2. Save the content
All non-code files must be stored here, such as icons, sounds, images, attribute lists, and text files.
Third, role
IOS sandbox provides good security for program running
Fourth, directory
1. Documents Directory: This directory is used to store user data or other information that should be backed up regularly, apple recommends that you store the file data created in the program or browsed in the program in this directory, which will be included during iTunes backup and recovery.
2. 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.
3. Library Directory: There are 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.
4. tmp directory: This directory is used to store temporary files, save unnecessary information during the application startup process again, and clear the files after restart.
Back up all Document and library files during synchronization between itues and iphone
5. Methods for obtaining different directories
1. Get the document directory
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
2. Obtain the cache directory
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
NSString * cachesDirectory = [paths objectAtIndex: 0];
3. Obtain the tmp directory path:
NSString * tmpDir = NSTemporaryDirectory ();
4. How to obtain the path of Chinese source files in the application package:
// For example, if an image resource (apple.png) is included in the program package:
NSString * imagePath = [[NSBundlemainBundle] pathForResource: @ "apple" ofType: @ "png"];
Vi. file I/O operations
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 (@ "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;
}



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.