iOS Development-sandboxed operations

Source: Internet
Author: User
Tags tmp folder

The iphone sandbox model has four folders, what is the location of the permanent data store, and what is the simple way to get the path to the emulator.
Documents,tmp,app,library.
(Nshomedirectory ()) Manually saved files in documents
Nsuserdefaults saved files in the TMP folder
1. Documents directory: You should write all the application data files to this directory. This directory is used to store user data or other information that should be backed up regularly.
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.

ways to get these directory paths:1, get the Home directory path function: NSString*homedir =nshomedirectory ();2, get the Documents directory path method: Nsarray*paths =nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString*docdir = [Paths Objectatindex:0];3, get the Caches directory path method: Nsarray*paths =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); NSString*cachesdir = [Paths Objectatindex:0];4, get the TMP directory path method: NSString*tmpdir =nstemporarydirectory ();5, get the path to the resource file in the application package by: For example, to get a picture resource (apple.png) path in the package: NSString*imagepath =[[NSBundle Mainbundle] pathforresource:@ "apple" oftype:@ "PNG"]; UIImage*appleimage = [[UIImage alloc] initwithcontentsoffile:imagepath];
The Mainbundle class method in the code is used to return an object that represents the application package.


Several ways to get the contents of the iphone Sandbox (sandbox):
Get the Sandbox home directory path
NSString *homedir = Nshomedirectory ();
Get the Documents directory path
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *docdir = [Paths objectatindex:0];
Get caches directory path
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
NSString *cachesdir = [Paths objectatindex:0];
Get the TMP directory path
NSString *tmpdir = Nstemporarydirectory ();

Gets a picture resource (apple.png) path in the current package
NSString *imagepath = [[NSBundle mainbundle] pathforresource:@ "apple" oftype:@ "PNG"];
UIImage *appleimage = [[UIImage alloc] initwithcontentsoffile:imagepath];

Example:
nsfilemanager* Fm=[nsfilemanager defaultmanager];if (![ FM fileexistsatpath:[self DataFilePath]) {    //The following is a saved path to the file    [FM createdirectoryatpath:[self DataFilePath ] Withintermediatedirectories:yes Attributes:nil Error:nil];    Get all filenames under a directory    nsarray *files = [FM subpathsatpath: [self datafilepath]];    Read a file    nsdata *data = [FM contentsatpath:[self DataFilePath];    or    nsdata *data = [NSData datawithcontentofpath:[self datafilepath];}

iOS Development-sandboxed operations

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.