Sandbox and NSBundle details, Sandbox nsbundle details

Source: Internet
Author: User

Sandbox and NSBundle details, Sandbox nsbundle details

Sandbox 1. Introduction to sandbox Mechanism

The Sandbox mechanism in iOS is a security system. Each iOS program has an independent File System (storage space) and can only be operated in the corresponding file system. This area is called a sandbox. The application must be in its own sandbox. Other applications cannot access the sandbox. All non-code files must be stored here, such as property files plist, text files, images, icons, and media resources. Sandbox is used to store data in the buffer zone. When the APP is turned off, the cache is automatically cleared. Sandbox path: NSLog (@ "% @", NSHomeDirectory ());

2. Sandbox directory structure

(1)/AppName. app package directory, containing the application itself. Because the application must be signed, you cannot modify the content in this directory at runtime. Otherwise, the application cannot be started.

(2)/Documents/save important data files and user data files of the application. User Data is basically stored in this location (for example, an image or music file downloaded from the internet). This folder is automatically backed up when the application is updated, you can also automatically synchronize and back up data when connecting to iTunes.

(3)/Library Directory: There are two sub-directories under this directory: You can create a sub-folder. It can be used to store data that you want to be backed up but not viewed by users. All folders in this path will be backed up by iTunes except Caches.

/Library/Caches (cache) stores the support files and cache files generated when the application is used (Save the information required during the application startup again ), it is recommended that log files be stored in this directory. This directory is not backed up during iTunes synchronization and may be cleared by other tools. /Library/Preferences (Preferences) is created when you save the Preferences setting file of the application (use NSUserDefaults (this is the default setting). You cannot manually create the Preferences. You can back up the Preferences in iTunes, the plist file is automatically backed up when the application is updated ).

(4)/tmp/Save the temporary data required for running the application. files in this path will not be backed up by iTunes. When the iphone restarts, all files in the directory will be automatically cleared. For example, after the video is recorded, it is stored in/temp/. Therefore, the video file should be transferred from this folder to the album, otherwise, the video file will be cleared by the system.

During synchronization with the iPhone, iTunes backs up all Documents and Library files.

// Obtain the path of Chinese source files in the application package: NSLog (@ "% @", [[NSBundlemainBundle] bundlePath]); NSString * imagePath = [[NSBundlemainBundle] pathForResource: @ "apple" ofType: @ "png"]; UIImage * appleImage = [[UIImagealloc] initWithContentsOfFile: imagePath];
3. NSSearchPathForDirectoriesInDomains

The NSSearchPathForDirectoriesInDomains method is used to find the Directory and returns the path set of the directory with the specified name within the specified range. There are three parameters

(1)DirectoryThe enum value of the NSSearchPathDirectory type indicates the name of the directory to be searched. For example, the NSDocumentDirectory indicates that we are searching for the Documents directory. If we replace it with NSCachesDirectory, it means we are searching for the Library/Caches directory.

(2)DomainMaskNSSearchPathDomainMask type enum value, specifying the search range. Here, NSUserDomainMask indicates that the search range is limited to the sandbox directory of the current application. It can also be written as NSLocalDomainMask (for/Library), NSNetworkDomainMask (for/Network), etc.

(3) expandTildeBOOL value, indicating whether to expand the wavy line. We know that the full write mode in iOS is/User/userName. If the value is YES, it indicates that the full write mode is used. If the value is NO, it indicates that the full write mode is directly written as "~".

The value is NO: Caches directory path ~ /Library/Caches the value is YES: the path of the Caches directory NSBundle

Bundle is a directory that contains the resources used by the program. These resources contain images, sounds, and compiled code, nib file (the user will also call the bundle plug-in), corresponding to the bundle, cocoa provides the class NSBundle

-Our program is a bundle. In the Finder, an application seems to be no different from other files, but actually it is a compilation code that contains the nib file, for other resource projects, we call this directory the main bundle of the program.

-Some resources in bundle can be localized, for example, foo. nib, we can have two versions: one for French users and one for English users, there will be two subdirectories in bundle: French. iproj and English. iproj. put the nib file in it. When the program needs to load the foo. nib file, the bundle automatically loads the file according to the set language.

Use the following method to obtain the main bundle of the program:
NSBundle * myBundle = [NSBundle mainBundle];

This method is usually used to obtain bundle. If you need resources from other directories, you can specify a path to obtain bundle.
NSBundle * goodBundle;
GoodBundle = [NSBundle bundleWithPath: @ "~ /. MyApp/Good. bundle "];

Once we have an NSBundle object, we can access the resources in it.
// Extension is optional
NSString * path = [goodBundle pathForImageResource: @ "Mom"];
NSImage * momPhoto = [NSImage alloc] initWithContentsOfFile: path];

-Bundle can also contain a library. If we get a class from the library, the bundle will connect to the library and find the class.

We can see that NSBundle has many functions, such as loading nib files...

Summary

1. NSLog (@ "NSBundle: % @", [NSBundle mainBundle]);
The return value of [NSBundle mainBundle] Is mainBundle, which corresponds to the NSBundle object in the directory where the executable file of the current program is located. If this bundle object cannot be created, the return value is nil. In general, mainBundle corresponds to the application package: a directory with the ". app" extension and application name as the base name.

2. Sandbox: security considerations. ios stores apps strictly in an independent location of the file system. This mechanism is called Sandbox)
NSBundle: it can be understood as the App package in the sandbox (for example, the package "App display Package content"); the App file is placed under the Bundle, in Xcode, the file path is obtained from the bundle. For example, in. caf is a short-effect audio file path, which is as follows: NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "in" ofType: @ "caf"];

Sandbox is used to isolate applications. Each application can only operate on its own sandbox.
// The bundle of the application is equivalent to a read-only sandbox environment.

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.