List saved files in iOS documents directory in a UITableView?

Here is the method I use to get the content of a directory.

-(NSArray *)listFileAtPath:(NSString *)path
{
    //-----> LIST ALL FILES <-----//
    NSLog(@"LISTING ALL FILES FOUND");

    int count;

    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    for (count = 0; count < (int)[directoryContent count]; count++)
    {
        NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);
    }
    return directoryContent;
}

-(NSArray *)findFiles:(NSString *)extension{

NSMutableArray *matches = [[NSMutableArray alloc]init];
NSFileManager *fManager = [NSFileManager defaultManager];
NSString *item;
NSArray *contents = [fManager contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil];

// >>> this section here adds all files with the chosen extension to an array
for (item in contents){
    if ([[item pathExtension] isEqualToString:extension]) {
        [matches addObject:item];
    }
}
return matches; }

The example above is pretty self-explanatory. I hope it answers you second question.


To get the contents of a directory

- (NSArray *)ls {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: documentsDirectory];

    NSLog(@"%@", documentsDirectory);
    return directoryContent;
}

To get the last path component,

[[path pathComponents] lastObject]

Thanks Alex,

Here is Swift version

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentDirectory = paths[0]
if let allItems = try? FileManager.default.contentsOfDirectory(atPath: documentDirectory) {
    print(allItems)
}

I know this is an old question, but it's a good one and things have changed in iOS post Sandboxing.

The path to all the readable/writeable folders within the app will now have a hash in it and Apple reserves the right to change that path at any time. It will change on every app launch for sure.

You'll need to get the path to the folder you want and you can't hardcode it like we used to be able to do in the past.

You ask for the documents directory and in the return array, it's at position 0. Then from there, you use that value to supply to the NSFileManager to get the directory contents.

The code below works under iOS 7 and 8 to return an array of the contents within the documents directory. You may want to sort it according to your own preferences.

+ (NSArray *)dumpDocumentsDirectoryContents {

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

    NSError *error;
    NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

    NSLog(@"%@", directoryContents);
    return directoryContents;
}