Several methods for obtaining file paths in iOS and ios
The iphone Sandbox Model has four folders: What are they, where permanent data storage is generally stored, and how to get the simulator path.
Documents, tmp, app, Library.
(NSHomeDirectory ()),
Manually saved files are in the documents file.
Nsuserdefaults stores the file 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 regularly backed up.
2. AppName. app Directory: This is the application package Directory, which contains the application itself. Because the application must be signed, you cannot modify the content in this directory during runtime. Otherwise, the application may fail to start.
3. Library Directory: There are two subdirectories: Caches and Preferences.
Preferences Directory: contains the preference setting file of the application. Instead of directly creating a preference file, you should use the NSUserDefaults class to obtain and set the preference of the application.
Caches Directory: used to store application-specific support files and save the information required when the application starts up again.
4. tmp directory: This directory is used to store temporary files and save unnecessary information when the application starts up again.
To obtain these directory paths:
1. Obtain the home directory path function:
NSString * homeDir = NSHomeDirectory ();
2. How to obtain the paths of the Documents directory:
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * docDir = [paths objectAtIndex: 0];
3. How to obtain the Caches directory path:
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
NSString * cachesDir = [paths objectAtIndex: 0];
4. Obtain the tmp directory path:
NSString * tmpDir = NSTemporaryDirectory ();
5. How to obtain the path of Chinese source files in the application package:
For example, you can obtain an image resource (apple.png) path in the program 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.
How to obtain several directories in the iphone sandbox:
[Cpp]View plaincopy
- // Obtain the main directory path of the sandbox.
- NSString * homeDir = NSHomeDirectory ();
- // Obtain the sources directory path
- NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
- NSString * docDir = [paths objectAtIndex: 0];
- // Obtain the Caches directory path
- NSArray * paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
- NSString * cachesDir = [paths objectAtIndex: 0];
- // Obtain the tmp directory path
- NSString * tmpDir = NSTemporaryDirectory ();
[Cpp]View plaincopy
- // Obtain the path of an image resource (apple.png) in the previous Program Package
- NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "apple" ofType: @ "png"];
- UIImage * appleImage = [[UIImage alloc] initWithContentsOfFile: imagePath];
Example:
NSFileManager * fm = [NSFileManager defamanager manager];
If (! [Fm fileExistsAtPath: [self dataFilePath]) {
// Below is the path for saving the file
[Fm createDirectoryAtPath: [self dataFilePath] withIntermediateDirectories: YES attributes: nil error: nil];
// Obtain all file names in a directory
NSArray * files = [fm subpathsAtPath: [self dataFilePath];
// Read an object
NSData * data = [fm contentsAtPath: [self dataFilePath];
// Or
NSData * data = [NSData dataWithContentOfPath: [self dataFilePath];
}