How IOS obtains various file directory paths _ios

Source: Internet
Author: User
Tags sqlite sqlite database tmp folder

The iphone sandbox model has four folders, what they are, where the permanent data store is typically placed, and what the simple way to get the emulator path is.

Documents,tmp,app,library.

(Nshomedirectory ()),

Manually saved files in documents

Nsuserdefaults saved files in the TMP folder

1. Documents Directory : You should write all de 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, or it may cause the application to fail to start.

3, Library directory : This directory has two subdirectories: 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 the application's preferences.
caches directory : Used to hold application-specific support files, and to save the information needed during application restart.

4. tmp directory : This directory is used to store temporary files and to save unwanted information when the application starts again.

How to get these directory paths:

The first way: Get the Home directory path function:

NSString *homedir = Nshomedirectory ();


The second way: Get the Documents directory path method:

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *docdir = [Paths objectatindex:0];

The third way: Get the Caches directory path:

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
NSString *cachesdir = [Paths objectatindex:0];

The fourth way: Get the method of the TMP directory path:

NSString *tmpdir = Nstemporarydirectory ();

The fifth way: Get the path to a resource file in an application package:
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 directory access methods in the iphone sandbox (sandbox):

Get the Sandbox home directory path

 
 

Get the Documents directory path

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); 

Get caches directory path

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); 

Get the TMP directory path

 
 

Gets a picture resource (apple.png) path in the current package

NSString *imagepath = [[NSBundle mainbundle] pathforresource:@ "apple" oftype:@ "PNG"]; 

Example:

nsfilemanager* Fm=[nsfilemanager Defaultmanager];
if (![ FM fileexistsatpath:[self DataFilePath]]) {

The following is a save on the path to making the file

[FM createdirectoryatpath:[self DataFilePath] Withintermediatedirectories:yes attributes:nil Error:nil];

Get all file names 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]];
}

There are several ways to get a file path for iOS, and here's an easy way to get a file path in iOS.

Online documnet and "tutorials" really make people more and more confused, or write it yourself.

First, place the file (such as the TestFile.txt file in this example) under the Resources group, and then write the code like this:

NSString *filepath = [[NSBundle mainbundle] pathforresource:@ "testfile" oftype:@ "txt"];

The output of the log in which you can see the testFile.txt path has been obtained.

One more example: Connecting to the SQLite database

NSString *datapath = [[NSBundle mainbundle] pathforresource:@ "MyData" oftype:@ "SQLite"];
if (Sqlite3_open ([DataPath utf8string], &db)!= Sqlite_ok)
{
  sqlite3_close (db);
  NSLog (@ "Database open failed");
}
else
{
  NSLog (@ "database opened successfully");
}

The above content is iOS obtains various file directory path the method, hoped to be helpful to everybody.

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.