Several methods for obtaining file paths in iOS and ios

Source: Internet
Author: User
Tags tmp folder

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
  1. // Obtain the main directory path of the sandbox.
  2. NSString * homeDir = NSHomeDirectory ();
  3. // Obtain the sources directory path
  4. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
  5. NSString * docDir = [paths objectAtIndex: 0];
  6. // Obtain the Caches directory path
  7. NSArray * paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES );
  8. NSString * cachesDir = [paths objectAtIndex: 0];
  9. // Obtain the tmp directory path
  10. NSString * tmpDir = NSTemporaryDirectory ();
[Cpp]View plaincopy
  1. // Obtain the path of an image resource (apple.png) in the previous Program Package
  2. NSString * imagePath = [[NSBundle mainBundle] pathForResource: @ "apple" ofType: @ "png"];
  3. 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];
}

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.