Ways to get directory paths for various files in iOS

Source: Internet
Author: User
Tags tmp folder

There are four folders in the phone sandbox model, 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 ()),

The manually saved files are in the documents file.

Nsuserdefaults saved files in the TMP folder


1, Documents directory: You should have all de Application data files are written to this directory. This directory is used to store user data or other should be periodically backup Span style= "Color:rgb (85,85,85);" > letter

package directory, containing application this So you cannot run the

3, Library Directory: This directory has two subdirectories: Caches and Preferences
preferences directory: includes application Span style= "Color:rgb (85,85,85);" > Preferences file. Instead of creating a preference file directly, you should use the Nsuserdefaults class to get and set the application preferences.
for storing application-specific Span style= "color: #000000;" > support file, save application need information.

4. tmp directory: This directory is used to store temporary files and to save information that is not needed during the application startup process.


Get these directory pathsof theMethod:
1, Get home directory pathof theFunction:
NSString *homedir = Nshomedirectory ();
2, get the Documents directory pathof theMethod:
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *docdir = [Paths objectatindex:0];
3, get the Caches directory pathof theMethod:
Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
NSString *cachesdir = [Paths objectatindex:0];
4, get the TMP directory pathof theMethod:
NSString *tmpdir = Nstemporarydirectory ();
5. Get the resource file path in the application packageof theMethod:
For example, get a picture resource (apple.png) path in the packageof theMethod:
NSString *imagepath = [[NSBundle mainbundle] pathforresource:@ "apple" oftype:@ "PNG"];
UIImage *appleimage = [[UIImage alloc] initwithcontentsoffile:imagepath];
In your codeof theThe Mainbundle class method is used to return a representative application packageof theObject.

Several ways to get the contents of the iphone Sandbox (sandbox):

[CPP] view plaincopy

  Get 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 ();  [cpp] view plaincopy//  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 save on the path to the file development
[FM createdirectoryatpath:[self DataFilePath] Withintermediatedirectories:yes attributes:nil Error:nil];

Get all the filenames in a directory
Nsarray *files = [FM subpathsatpath: [self datafilepath]];

Read a file
NSData *data = [FM contentsatpath:[self DataFilePath];

Or
NSData *data = [NSData datawithcontentofpath:[self DataFilePath];
}


This article is from "Xiao Liu _blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1566879

Ways to get directory paths for various files in iOS

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.