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.