IOS file operations: SandBox, package (NSBundle), sandboxnsbundle

Source: Internet
Author: User

IOS file operations: SandBox, package (NSBundle), sandboxnsbundle
Reprinted please declare Source: http://blog.csdn.net/jinnchang/article/details/44828189
1. Introduction to the SandBox mechanism in iOS is a security system.
Each iOS application has a separate file system (storage space) and can only be operated in the corresponding file system. This area is called a sandbox. All non-code files must be stored here, such as property files plist, text files, images, icons, and media resources.
2. Sandbox directory structure

Generally, each sandbox contains the following directories and files:

  • /AppName. app package directory. Because the application must be signed, you cannot modify the content in this directory at runtime. Otherwise, the application cannot be started.
  • /Documents/Stores important data files and user data files of the application. This directory is backed up during iTunes synchronization.
  • /Library/Caches stores the support files and cache files generated when the application is used. It is best to put the log files in this directory. This directory is not backed up during iTunes synchronization.
  • /Library/Preferences: Save the preference setting file of the application (created when the NSUserDefaults class is used, and should not be created manually ).
  • /Tmp/Save the temporary data required for running the application. When the iphone restarts, all files in the directory will be cleared.

Shows the directory structure:


Supplement 1: The preceding description can be used as an example. For a notepad application, you need to save the content you write. The content is generated by the user and must be placed in the Documents directory. If a news application needs to download something from the server and show it to the user, the downloaded content will be placed in the Library/Caches directory. Apple has strict requirements for this review, mainly because of the synchronization problem of iCloud.

Supplement 2: If you want to know the path of the real machine or simulator App sandbox, you can print the following code in the project:
let homeDir = NSHomeDirectory() as Stringprintln(homeDir)
3. Obtain the directory paths in the sandbox.
// 1. Obtain the sandbox root directory let homeDir = NSHomeDirectory () as String // 2. Obtain the logs directory let docDirs = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory. documentDirectory, NSSearchPathDomainMask. userDomainMask, true) as NSArraylet docDir = docDirs [0] as String // 3. Obtain the Caches directory let cachesDirs = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory. cachesDirectory, NSSearchPathDomainMask. userDomainMask, true) as NSArraylet cachesDir = cachesDirs [0] as String // 4. Obtain the Library directory let libDirs = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory. libraryDirectory, NSSearchPathDomainMask. userDomainMask, true) as NSArraylet libDir = libDirs [0] as String // 5. Obtain the tmp directory let tmpDir = NSTemporaryDirectory () as String
4. NSFileManager uses fileManager to operate directories and files in the sandbox.

// 1. obtain the Documents directory let docDirs = NSSearchPathForDirectoriesInDomains (NSSearchPathDirectory. documentDirectory, NSSearchPathDomainMask. userDomainMask, true) as NSArraylet docDir = docDirs [0] as String // 2. initialize fileManagerlet fileManager = NSFileManager. defaultManager () // 3. Use fileManager to create the directory let testDir = docDir + "/test" fileManager. createDirectoryAtPath (testDir, withIntermediateDirectories: true, attributes: nil, error: nil) // 4. Use fileManager to create the file let testFile1 = testDir + "/test1.txt" let testFile2 = testDir + "/test2.txt" fileManager. createFileAtPath (testFile1, contents: nil, attributes: nil) fileManager. createFileAtPath (testFile2, contents: nil, attributes: nil) // 5. Use fileManager to obtain the file name var files = fileManager in the directory. subpathsAtPath (testDir) // 6. Use fileManager to delete the file fileManager. removeItemAtPath (testFile1, error: nil)

The final directory result of the above Code is shown in:


5. Package (NSBundle)

IOS applications are encapsulated by bundle, which can be narrowly understood as the AppName. app file in the above sandbox. In the Finder, bundle is displayed as a file to prevent program file corruption caused by user misoperations. However, it is actually a directory, contains images, media resources, compiled code, and nib files. This directory is called main bundle.

Is the internal directory structure of the application package of a newly created SandBoxSample project:

Cocaoa provides the NSBundle class to encapsulate bundle operations.

// Obtain the application's main bundlevar mainBundle = NSBundle. mainBundle () // use the main bundle to obtain the resource path var testFilePath = mainBundle. pathForResource ("Info", ofType: "plist ")
6. Conclusion

Last article updated: 09:53:39, January 1, April 2, 2015. The references are as follows:

About Files and Directories

NSFileManager Class Reference

NSBundle Class Reference

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.