Search code examples
iosnsfilemanager

How can I return all the name of the files in Resources folder in ios?


I want to retrieve in an array of the name of the files in Resources folder that have the .html extension. I have done this:

NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Resources"];
NSError *error = nil;
NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
for (int i=0; i < [documentArray count] - 1; i++) {
    NSLog(@"value %@", [documentArray objectAtIndex:i]);
}

But the for loop displays continously null. Anyone knows how can I get this task right? thank you


Solution

  • Don't use NSHomeDirectory. Use [[NSBundle mainBundle] resourcePath] to get path to app's resources.

    Edited: here's your example code.

    NSString *resPath = [[NSBundle mainBundle] resourcePath];
    NSError *error = nil;
    NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resPath error:&error];
    if (!error)
    {
        for (NSString * filename in filenames)
        {
            NSString *extension = [filename pathExtension];
            if ([extension isEqualToString:@"html"])
            {
                NSLog(@"%@", filename);
            }
        }
    }