Swift IOS file operation: Sandbox (sandbox), package (NSBundle)

Source: Internet
Author: User

1. Sandbox mechanism Introduction 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 saved here, such as property files plist, text files, images, icons, Media Resources and more.
2. Sandbox directory structure

Typically, each sandbox contains the following directories and files:

    • /appname. The package directory for the app 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:
Let Homedir = Nshomedirectory () as Stringprintln (Homedir)
3. Get the directory paths in the sandbox
1. Get the Sandbox root directory let Homedir = Nshomedirectory () as string//2, get the Documents directory let Docdirs = Nssearchpathfordirectoriesindomains (. Documentdirectory,. Userdomainmask, True) as Nsarraylet Docdir = Docdirs[0] as string//3, get Caches directory let Cachesdirs = Nssearchpathfordirector Iesindomains (. Cachesdirectory,. Userdomainmask, True) as Nsarraylet Cachesdir = Cachesdirs[0] As string//4, get Library directory let Libdirs = Nssearchpathfordire Ctoriesindomains (. Librarydirectory,. Userdomainmask, True) as Nsarraylet Libdir = Libdirs[0] As string//5, get tmp directory let TmpDir = Nstemporarydirectory () as Stri Ng
4, Nsfilemanageryou can use FileManager to manipulate directories and files in the sandbox.

1. Get the Documents directory let Docdirs = Nssearchpathfordirectoriesindomains (. Documentdirectory,. Userdomainmask, True) as Nsarraylet Docdir = Docdirs[0] as string//2, initialize filemanagerlet FileManager = Nsfilemanager.defau Ltmanager ()//3, create directory with filemanager let TestDir = Docdir + "/test" Filemanager.createdirectoryatpath (TestDir, Withintermediatedirectories:true, Attributes:nil, Error:nil)//4, create file with filemanager let TestFile1 = TestDir + "/test1.t XT "Let TestFile2 = TestDir +"/test2.txt "Filemanager.createfileatpath (TestFile1, Contents:nil, Attributes:nil) Filemanager.createfileatpath (TestFile2, Contents:nil, Attributes:nil)//5, using FileManager to get the file name under directory var files = Filemanager.subpathsatpath (TestDir)//6, delete files using FileManager Filemanager.removeitematpath (TestFile1, Error:nil)

The final catalog results for the above code are as follows:

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.

Get the application's main bundlevar mainbundle = Nsbundle.mainbundle ()//Use the main bundle to get the resource path var Testfilepath = Mainbundle.pathforr Esource ("logo", OfType: "PNG")
6. Conclusion

Article last updated: April 2, 2015 09:53:39. The resources are as follows:

About Files and directories

Nsfilemanager Class Reference

NSBundle Class Reference

Swift IOS file operation: Sandbox (sandbox), package (NSBundle)

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.