Get the names of files in an iCloud Drive folder that haven't been downloaded yet

Viewed 646

I’m trying to get the names of all files and folders in an iCloud Drive directory:

import Foundation

let fileManager = FileManager.default
let directoryURL = URL(string: "folderPathHere")!

do {
    let directoryContents = try fileManager.contentsOfDirectory(at: directoryURL, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles])
    for url in directoryContents {
        let fileName = fileManager.displayName(atPath: url.absoluteString)
        print(fileName)
    }
} catch let error {
    let directoryName = fileManager.displayName(atPath: directoryURL.absoluteString)
    print("Couldnt get contents of \(directoryName): \(error.localizedDescription)")
}

It appears that any iCloud files that haven’t been downloaded to the device don’t return URLs.

I know I can check if a path contains a ubiquitous item when I already know the path with the code below (even if it isn’t downloaded):

fileManager.isUbiquitousItem(at: writePath)

Is there a way to get the URLs & names of those iCloud files without downloading them first?

The directory URL is a security-scoped URL constructed from bookmark data in case that makes any difference (omitted that code here for clarity).

Thanks

1 Answers

Found the answer. I was skipping hidden files with ".skipsHiddenFiles", but the non-downloaded files are actually hidden files, named: ".fileName.ext.iCloud".

Remove the skips hidden files option now works as expected.

Related