List saved files in iOS document directory in UITableView?

I created the following code to save the file in the document directory:

NSLog(@"Saving File..."); NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.reddexuk.com/logo.png"]]; AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"logo.png"]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Successfully downloaded file to %@", path); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [operation start]; 

However, I want to add each file to the UITableView when it is successfully saved. When a file in the UITableView is connected, I would like the UIWebView to navigate to this file (everything is offline).

Also - how can I get the file name and complete it, for example, "logo.png" instead of http://www.reddexuk.com/logo.png ?

How can i do this?

+55
ios objective-c iphone xcode afnetworking
Dec 04 '11 at 15:58
source share
9 answers

Here is the method I use to get the contents 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; } 
+143
Dec 04 '11 at 16:08
source share
 -(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 above example is pretty clear. I hope he answers the second question.

+21
Dec 04 '11 at 16:30
source share

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 component of the path,

 [[path pathComponents] lastObject] 
+11
Dec 04 '11 at 16:10
source share

Thanks, Alex,

Here is the version of Swift

 func listFilesOnDevice() { let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentDirectory = paths[0] as! String let manager = NSFileManager.defaultManager() if let allItems = manager.contentsOfDirectoryAtPath(documentDirectory, error: nil) { println(allItems) } } 
+8
Apr 19 '15 at 7:15
source share

For a more reasonable file name:

NSString *filename = [[url lastPathComponent] stringByAppendingPathExtension:[url pathExtension]];

+4
Dec 06 '11 at 4:50
source share

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

The path to all readable / writable folders in the application will now have a hash, and Apple reserves the right to change this path at any time. It will change every time the application starts.

You will need to get the path to the folder you want, and you cannot hardcode it, as we used to do in the past.

You request the document catalog and the returned array, it is at position 0. Then from there you use this value to provide the NSFileManager to get the contents of the catalog.

Below is the code below in iOS 7 and 8 to return an array of content to the document directory. You can sort it according to your preference.

 + (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; } 
+4
Apr 16 '15 at 18:07
source share
 NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:dir_path]; NSString *filename; while ((filename = [dirEnum nextObject])) { // Do something amazing } 

for listing through ALL files in a directory

+1
Sep 13 '15 at 16:12
source share

Swift 3.x

 let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] if let allItems = try? FileManager.default.contentsOfDirectory(atPath: documentDirectory) { print(allItems) } 
+1
Jul 31 '17 at 14:12
source share

Swift 5

A function that returns an array of URLs of all files in the Documents directory, which are MP4 videos. If you want all the files, just delete the filter .

It only checks files in the top directory. If you also want to list files in subdirectories, remove the .skipsSubdirectoryDescendants option.

 func listVideos() -> [URL] { let fileManager = FileManager.default let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] let files = try? fileManager.contentsOfDirectory( at: documentDirectory, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles] ).filter { $0.lastPathComponent.hasSuffix(".mp4") } return files ?? [] } 
0
Apr 19 '19 at 21:54
source share



All Articles