List saved files in iOS documents directory in a UITableView?

前端 未结 9 1693
予麋鹿
予麋鹿 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

    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 ?? []
    }
    

提交回复
热议问题