SoFunction
Updated on 2025-04-13

Summary of basic methods for accessing and managing file directories in iOS development

Access to file directory

The simplest: (Since it is a sandbox relationship, there is no concept of folders)

Copy the codeThe code is as follows:

UIImage* image = [UIImage imageNamed:@""];

This is a path that is packaged in the app, and there is no need to add additional paths.


Another type is that you need to specify the file path:

NSBundle is required.

Copy the codeThe code is as follows:

[[NSBundle mainBundle] resourcePath], this is the packaged path of the program.

If you need to specify a path, you have to write it like this:

You can also splice it yourself:

Copy the codeThe code is as follows:

NSString* path = [NSStringstringWithFormat:@"%@/%@/%@",[[NSBundlemainBundle] resourcePath],@"document",@""];

Or directly:
Copy the codeThe code is as follows:

NSString* path = [[NSBundle mainBundle] pathForResource:@"aaa" ofType:@"png"];

Generally speaking, the application has 3 directories

Documents,Library,tmp

Now Apple does not allow big data to be saved in documents. If you want to save videos or something in documents, it is more troublesome to backup to iouch.

The general method of nesting is to save it in the library caches directory (I don’t know if it is reasonable:))

In the tmp directory, save some temporary files and you can delete the cache contents in it when you exit the program.


Get your own documents directory in the application:

Copy the codeThe code is as follows:

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString * documentDirectory = [paths objectAtIndex:0];

Based on the above, get a complete file path and name:
Copy the codeThe code is as follows:

NSString * file = [documentDirectory stringByAppendingPathComponent:@""];

This allows you to create, read, and write files with files.

File directory management
Here are some commonly used file directory management methods
1. Common NSFileManager file methods

Copy the codeThe code is as follows:

-(NSData *)contentsAtPath:path//Read data from a file


-(BOOL)createFileAtPath: path contents:(NSData *)data attributes:attr//Write data to a file


-(BOOL)removeItemAtPath:path error:err//Delete a file


-(BOOL)moveItemAtPath: from toPath:to error:err//Rename or move a file (to cannot be existing)

-(BOOL)copyItemAtPath:from toPath:to error:err//Copy the file (to cannot be existing)


-(BOOL)contentsEqualAtPath:path andPath:path2//Compare the contents of two files


-(BOOL)fileExistAtPath:path//Test whether the file exists


-(BOOL) isReadableFileAtPath:path//Test whether the file exists and whether it can perform read operations


-(BOOL) isWriteableFileAtPath:path//Test whether the file exists and whether the write operation can be performed


-(NSDictionary *) attributesOfItemAtPath:path error:err//Get the file's attributes


-(BOOL)setAttributesOfItemAtPath:attr error:err//Change the properties of the file


2.Using Directory
Copy the codeThe code is as follows:

-(NSString *)currentDirectoryPath//Get the current directory


-(BOOL)changeCurrentDirectoryPath:path//Change the current directory


-(BOOL)copyItemAtPath:from toPath:to error:err//Copy the directory structure (to cannot be existing)


-(BOOL)createDirectoryAtPath:path withIntermediateDirectories:(BOOL)flag attribute:attr//Create a new directory


-(BOOL)fileExistAtPath:path isDirectory:(BOOL*)flag//Test the file is a directory (Storing the result YES/NO in the flag)


-(NSArray *)contentsOfDirectoryAtPath:path error:err//list directory contents


-(NSDirectoryEnumerator *)enumeratorAtPath:path//Enumerator Contents of the enumeration directory


-(BOOL)removeItemAtPath:path error:err//Delete empty directory


-(BOOL)moveItemAtPath:from toPath:to error:err //Rename or move a directory (to cannot be existing)


3. Common path tools
Copy the codeThe code is as follows:

+(NSString *)pathWithComponens:components//Construct valid paths based on elements in components


-(NSArray *)pathComponents//Destruct the path and obtain the various parts that make up this path


-(NSString *)lastPathComponent// Extract the last component of the path
-(NSString *)pathExtension// Extract its extension from the last component of the path


-(NSString *)stringByAppendingPathComponent:path//Add path to the end of an existing path


-(NSString *)stringByAppendingPathExtension:ext//Add the specified extension to the last component of the path

-(NSString *)stringByDeletingLastPathComponent//Delete the last component of the path


-(NSString *)stringByDeletingPathExtension//Delete the extension from the last part of the file

-(NSString *)stringByExpandingTileInPath //Expand the character in the path into the user's home directory (~) or the specified user's home directory (~user)

-(NSString *)stringByresolvingSymlinksInPath//Try to resolve symbolic links in the path

-(NSString *)stringByStandardizingPath//Normalize the path by trying to parse ~, .. (parent directory symbol), . (current directory symbol) and symbolic links


4. Commonly used path tool functions
Copy the codeThe code is as follows:

NSString* NSUserName(void)//Return the login name of the current user


NSString* NSFullUserName(void)//Return the full user name of the current user


NSString* NSHomeDirectory(void)//Return the path to the current user home directory


NSString* NSHomeDirectoryForUser(NSString* user)//Return to the home directory of the user user


NSString* NSTemporaryDirectory(void)//Returns the path directory that can be used to create temporary files


5. Commonly used IOS directory
Copy the codeThe code is as follows:

Documents (NSDocumentDirectory) // The directory used to write application-related data files. The files written here in ios can be shared and accessed with iTunes. The files stored here will be automatically backed up to the cloud.

Library/Caches (NSCachesDirectory)// is used to write to the directory of the application supporting files and save the information needed for the application to start again. iTunes will not back up the contents of this directory

tmp(use NSTemporaryDirectory())//This directory is used to store temporary files. Only these files need to be removed when the program terminates. When the application no longer needs these temporary files, it should be deleted from this directory.

Library/Preferences//This directory contains the application's preferences file. Use the NSUserDefault class to create, read and modify the preferences file.