How to test the existence of a file with NSFileManager

Here’s how you can check if a file exists in your app’s Documents directory:

NSURL *yourFile = [[self applicationDocumentsDirectory]URLByAppendingPathComponent:@"YourFileHere.txt"];
if ([[NSFileManager defaultManager]fileExistsAtPath:storeFile.path isDirectory:NO]) {

    NSLog(@"The file DOES exist");

} else {

    NSLog(@"The file does NOT exist");

This example relies on another method which is usually part of the AppDelegate.m file called applicationDocumentsDirectory. It will return an NSURL to your Documents directory. Just in case you’re working on a project which does not include it, here it is:

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory

    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

Leave a Reply