0

I have the following code to enumerate through a directory. The URL to the directory is passed in as a parameter to the function.

The startAccessingSecurityScopedResource returns true at the start of the function, however when I start to enumerate it returns false for all the following files and subdirectories inside the original folder selected by the user.

This is the code for the function.

func readDirectory(file: URL){
    
    let localFileManager = FileManager()
    let resourceKeys = Set<URLResourceKey>([.nameKey, .isDirectoryKey])
    let directoryEnumerator = localFileManager.enumerator(
        at: file,
        includingPropertiesForKeys: Array(resourceKeys),
        options: .skipsHiddenFiles
    )!

    
    for case let fileURL as URL in directoryEnumerator {
        guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
            let isDirectory = resourceValues.isDirectory,
            let name = resourceValues.name else {
            continue
        } 
    }
}

From the above code, the file parameter passed does return true for access. However, in the directoryEnumerator loop, the fileURL doesn't have access.

Is there a reason that I am losing access to the subdirectories and files inside the original directory that it says I do have access to?

Alexander
  • 59,041
  • 12
  • 98
  • 151
Saad Mahboob
  • 21
  • 1
  • 3
  • Unrelated: but `Array(resourceKeys)` doesn't make much sense, given that you explicitly made `resourceKeys` a `Set`. You can just use `let resourceKeys: [URLResourceKey] = [.nameKey, .isDirectoryKey]`, and pass that array directly to `enumerator(at:includingPropertiesForKeys:options:)` and `resourceValues(forKeys:)`. – Alexander Dec 01 '21 at 17:28
  • Have you found an answer to it? I am in a similar situation now – Bardt Oct 23 '22 at 11:23

0 Answers0