This article provides a comprehensive overview of the iOS app sandbox model's folders and their usage scenarios, as well as ways to get these folders through Swift
Introduction to the sandbox mechanism in IOS
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.
Four folder descriptions and features for the iphone sandbox model
1. Documents directory: You should write all the de application data files to this directory. This directory is used to store user data or other information that should be backed up regularly. files saved by the user in the documents file
2. Appname.app directory: This is the application's package directory, which contains the application itself. Because the application must be signed, you cannot modify the contents of this directory at run time, which may cause the application to fail to start.
3. Library directory: There are two sub-directories under this directory: Caches and Preferences
Preferences directory: Contains the application's preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set your application's preferences.
Caches directory: Used to hold application-specific support files, saving the information that is needed during application startup.
4. tmp directory: This directory is used to store temporary files, saving information that is not needed during the application restart.
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:
ways to get these directory paths1, get the Home directory path function:
Let Homedir = Nshomedirectory ()
2. Method for obtaining the Documents directory path:
Let paths = Nssearchpathfordirectoriesindomains (nssearchpathdirectory. nsdocumentdirectory , Nssearchpathdomainmask . Userdomainmask, true)
Let document Path = Paths[0]
3, get the Caches directory path method:
Let paths = Nssearchpathfordirectoriesindomains (nssearchpathdirectory. cachesdirectory , Nssearchpathdomainmask . Userdomainmask, true)
Let CachePath = Paths[0]
4. Method to get the TMP directory path:
Let TmpDir = Nstemporarydirectory ();
5, gets the method of the resource file path in the application package:
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 Cocoa provides a nsbundle class that encapsulates the bundle operation.
Various resource files can be obtained through nsbundle, note that this resource file is required under the build Phases-copy Bundle rresources, and some types of files are not automatically added here
let path = nsbundle. Mainbundle (). Pathforresource ("20160504182134006", OfType: "PCM")
The Mainbundle class method in the code is used to return an object that represents the application package.
Introduction to Nsfilemanager file Managementyou can use FileManager to manipulate directories and files in the sandbox.
Example:
var Fm:nsfilemanager = Nsfilemanager.defaultmanager ()
If!fm.fileexistsatpath (Self.datafilepath ()) {
The following is a save on the path to the file development
Fm.createdirectoryatpath (Self.datafilepath (), Withintermediatedirectories:true, Attributes:nil, Error:nil)
Get all the filenames in a directory
var files: [Anyobject] = Fm.subpathsatpath (Self.datafilepath ())
Read a file
var data:nsdata = Fm.contentsatpath (Self.datafilepath ())
Or
var data:nsdata = Nsdata.datawithcontentofpath (Self.datafilepath ())
}
IOS app sandbox model file directory in detail Swift version