Access all files in UIDocumentPickerController while picking folder

Hello,

I need to get all files (recursively) from an iCloud Drive folder.

So I use UIDocumentPickerController with UTType.folder.

Then I use FileManager to get all files from the picked folder, but when I try to access it with startAccessingSecurityScopedResource it return false.

It's work perfectly if I dont get files by FileManager but directly by the UIDocumentPickerController when configured for multiple files selection.

How can I access to all files from a picked directory with permission granted ? Is it at least possible ?

Thanks

Replies

Then I use FileManager to get all files from the picked folder, but when I try to access it with startAccessingSecurityScopedResource it return false.

I’d like to clarify the first “it” in the above sentence. The way I’d expect this to work is:

  1. You get the directory URL from the document picker.

  2. You call startAccessingSecurityScopedResource() on that URL.

  3. You access stuff within that directory.

Is that what you’re doing? Meaning that the call in step 2 is returning false?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Let me give more precisions: If I call startAccessingSecurityScopedResource on the URL of the directory, it's working (return true).

The startAccessingSecurityScopedResource returning false happen when I call it on a file URL.

So:

  1. First, I get the directory URL with UIDocumentPickerViewController configured with .folder as UTTYpe.
  2. I try to list recursively all files in this directory, using FileManager with code below (working)
func listFilesRecursively(at url: URL) -> [URL] {
        var newUrls: [URL] = url.hasDirectoryPath ? [] : [url]
        if url.hasDirectoryPath && url.startAccessingSecurityScopedResource() {
            let fileManager = FileManager.default
            let enumerator = fileManager.enumerator(at: url, includingPropertiesForKeys: nil)
            while let fileURL = enumerator?.nextObject() as? URL {
                newUrls.append(fileURL)
            }
            url.stopAccessingSecurityScopedResource()
        }
        return newUrls
    }
  1. I try to process each file's URL to read files in my app, making a startAccessingSecurityScopedResource on file's URL. It's where I'm getting false as returned value.

OK. So startAccessingSecurityScopedResource() is returning false for a nested file, but can you access access the file?

I’d expect that you should be able to, because you successfully called startAccessingSecurityScopedResource() on the parent directory.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"