List saved files in iOS documents directory in a UITableView?

前端 未结 9 1661
予麋鹿
予麋鹿 2020-11-30 18:29

I have set up the following code to save a file to the documents directory:

NSLog(@\"Saving File...\");

NSURLRequest *request = [NSURLRequest requestWithURL         


        
相关标签:
9条回答
  • 2020-11-30 19:04

    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]
    
    0 讨论(0)
  • 2020-11-30 19:04

    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)
    }
    
    0 讨论(0)
  • 2020-11-30 19:04

    Swift 5

    Function that returns array of URLs of all files in Documents directory that are MP4 videos. If you want all files, just remove the filter.

    It checks only files in the top directory. If you want to list also files in the 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 讨论(0)
  • 2020-11-30 19:06

    Swift 3.x

    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    if let allItems = try? FileManager.default.contentsOfDirectory(atPath: documentDirectory) {
      print(allItems)
    }
    
    0 讨论(0)
  • 2020-11-30 19:08

    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;
    }
    
    0 讨论(0)
  • 2020-11-30 19:11
    NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:dir_path];
    
    NSString *filename;
    
    while ((filename = [dirEnum nextObject]))
    {
        // Do something amazing
    }
    

    for enumerating through ALL files in directory

    0 讨论(0)
提交回复
热议问题