IOS file operations: Sandbox, File Operations (FileManager), Packages (NSBundle)

Source: Internet
Author: User
Tags vars

http://blog.csdn.net/jinnchang/article/details/44828189

1. Introduction of sandbox mechanism

The sandbox mechanism (sandbox) in IOS is a security system.
Each IOS application has a separate file system (storage space) and can only operate on the corresponding file system, which is called a sandbox. All non-code files are stored here, such as property file plist, text files, images, icons, media resources, and so on.

2. Sandbox directory structure

Typically, each sandbox contains the following directories and files:

    • /appname.app The package directory for the application. Because the application must be signed, the content in this directory cannot be modified at run time, or the application will not start.
    • /documents/save important data files and user data files for your application. The directory is backed up when ITunes syncs.
    • /library/caches Save the support files and cache files that are generated when the application is used, and the log files are best placed in this directory. ITunes does not back up this directory when syncing.
    • /library/preferences saves the application's preferences file (created when using the Nsuserdefaults class settings and should not be created manually).
    • /tmp/saves the temporary data needed to run the app, and when the iphone restarts, it clears all files in that directory.

The directory structure looks like this:

Add 1: For the above description can be such an example of understanding, a Notepad application, the user write things need to be saved, these things are generated by the user, you need to put in the Documents directory. A news app, if you need to download something from the server to show the user, the downloaded items are placed in the Library/caches directory. The Apple audit is strict with this requirement, mainly because of the ICloud sync issue.

Supplement 2: If you want to know the real machine or simulator App sandbox path, you can get it by executing the following code in the project:

[OBJC]View PlainCopy
    1. NSString *homedirectorypath = Nshomedirectory ();
3. Get the directory paths in the sandbox [OBJC]View PlainCopy
  1. Get the Sandbox root directory path
  2. NSString *homedirectorypath = Nshomedirectory ();
  3. Get Applications Path
  4. NSString *appdirectorypath = [Nssearchpathfordirectoriesindomains (nsapplicationdirectory, NSUserDomainMask, YES  ) Objectatindex:0];
  5. Get Documents Path
  6. NSString *documentdirectorypath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask,   YES) Objectatindex:0];
  7. Get Caches Path
  8. NSString *cachesdirectorypath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, NSUserDomainMask, YES)  Objectatindex:0];
  9. Get the TMP path
  10. NSString *tmpdirectorypath = Nstemporarydirectory ();
4, Nsfilemanager

You can use FileManager to manipulate directories and files in the sandbox. A single example of Nsfilemanager can be obtained by the following method:

[OBJC]View PlainCopy
    1. [Nsfilemanager Defaultmanager]
5. Package (NSBundle)

IOS apps are encapsulated by bundles and can be narrowly understood as appname.app files in the sandbox. In the Finder, the bundle as a file display so as to prevent user misuse caused the program file corruption, but in fact, inside is a directory, including images, media resources, compiled code, NIB files, etc., this directory is called the main bundle.

The COCAOA provides a nsbundle class that encapsulates the bundle operation.

[OBJC]View PlainCopy
      1. Get the main bundle of the application
      2. NSBundle *mainbundle = NSBundle. Mainbundle;
      3. Get the resource path using the main bundle
      4. NSString *filepath = [Mainbundle pathforresource:@ "logo" ofType:@ "PNG"];

IOS file operations: Sandbox, File Operations (FileManager), Packages (NSBundle)

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.