iOS File Management

When an iOS App is installed on a device, a folder structure will be created, including a tmp directory for storing temporary data. In this tutorial we are managing files in this tmp directory such as creating, reading and deleting files.

Open Xcode and create a new Single View Application. For product name, use FileManagementDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards is deselected and Use Automatic Reference Counting checkboxes are selected.

Go to Viewcontroller.xib and drag four buttons to the view. Give the buttons the following titles:

  • Create File
  • List File
  • Read File
  • Delete File

The view should now look like this

in ViewController.m, declare two properties

@property (nonatomic, strong) NSFileManager *fileManager;
@property (nonatomic, strong) NSString *tmpDir;

The fileManager property enables us to perform file-operations. The tmpdir property will point to the tmp directory of our app. Next declare the IBActions

- (IBAction)createFile:(id)sender;
- (IBAction)deleteFile:(id)sender;
- (IBAction)listFile:(id)sender;
- (IBAction)readFile:(id)sender;

Go back to ViewController.xib and crtl-click and drag from each button to the File's Owner placeholder and select the appropriate IBAction method.

In ViewController.m, implement the viewDidLoad method

- (void)viewDidLoad 
[super viewDidLoad];
self.fileManager = [[NSFileManager alloc] init];

// tmp Directory
self.tmpDir = NSTemporaryDirectory();

The FileManager object is initialized and the tmpdir property now points to the tmp Directory of our app. Next implement the IBAction methods.

- (IBAction)createFile:(id)sender 
NSString *fileName = @"sample.txt";
NSString *path = [self.tmpDir stringByAppendingPathComponent:fileName];
NSString *contentsOfFile = @"Sample text.";

// Write File [contentsOfFile writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"File sample.txt created at tmp directory");

The sample.txt file is created and written to the tmp directory with the writeToFile:atomically:encoding:error method.

- (IBAction)deleteFile:(id)sender 
NSString *path = [self.tmpDir stringByAppendingPathComponent: [self enumerateDirectory]];

// Delete File
[self.fileManager removeItemAtPath:path error:nil];

The enumerateDirectory method is a helper method which enumerates the tmp Directory and returns a filename if found. We will implement this method later. The removeItemAtPath:error method deletes the file.

- (IBAction)listFile:(id)sender 
// List Contents of Path
NSLog(@"contents of path %@ = \n%@", self.tmpDir, [self enumerateDirectory]);

This method simply list the contents of the given path.


- (IBAction)readFile:(id)sender 
NSString *path = [self.tmpDir stringByAppendingPathComponent: [self enumerateDirectory]];
NSString *contentsOfFile = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// List Contents of File
NSLog(@"Contents of file = %@", contentsOfFile);

The initWithContentsOfFile:encoding:error method puts the content of a file into a NSString. Last, Implement the enumerateDirectory helper method.

- (NSString *)enumerateDirectory 
NSArray *filesInDirectory = [self.fileManager contentsOfDirectoryAtPath:self.tmpDir error:nil];
if ([filesInDirectory count] > 0)
for (NSString *fileName in filesInDirectory)
if ([fileName isEqualToString:@"sample.txt"])
return fileName;
NSLog(@"no file created yet.");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"iOS File Management" message:@"No File Created Yet" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return nil;
return nil;

All files inside the tmp Directory will be put into a NSArray and this array will be checked for the existence of our sample.txt file. When succesful, the filename will be returned. Otherwise a AlertView will be displayed.

Build and Run, try our button and look at the console to see the results of each action.

You can download the source code of the FileManagementDemo at the ioscreator repository on github.