Question

I'm looking to return an NSArray which contains all of the files URLS located within a particular folder in iOS.

In this case I'm looking inside the Ryan/Bob/FolderWithInfo - which contains 4 files. I want to get the file URL of each of these files.

Here is a example of an output I'm looking for :

NSArray *array = @[

@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout1.csv",
@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout2.csv",
@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout3.csv",
@"file:///var/mobile/Applications/8D5EBBB2-6726-4FE9-95CB-453635443643/Documents/Ryan/Bob/FolderWithInfo/Layout4.csv",
];

How is this achievable?

Edit: I have tried this and thought of maybe prepending the main path after, but there has to be a more efficient way :)

NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
directory = [NSString stringWithFormat:@"%@/%@/%@/%@", directory, @"Ryan, @"Bob", @"FolderWithInfo"];

NSArray *files=[fileMgr contentsOfDirectoryAtPath:directory error:&error];
NSLog(@"mag1 directory: %@",files);

Thanks.

Was it helpful?

Solution

As said in the comments, use - (NSArray *)contentsOfDirectoryAtURL:(NSURL *)url includingPropertiesForKeys:(NSArray *)keys options:(NSDirectoryEnumerationOptions)mask error:(NSError **)error.

NSFileManager *fm = [NSFileManager defaultManager];

NSError *error;
NSURL *url = [NSURL URLWithString:@"file:///Users/leandros/temp"];
NSArray *urls = [fm contentsOfDirectoryAtURL:url includingPropertiesForKeys:nil options:0 error:&error];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top