iCloudDocumentSync icon indicating copy to clipboard operation
iCloudDocumentSync copied to clipboard

Folder support

Open MaxHasADHD opened this issue 10 years ago • 3 comments

Can you add folder support? Currently It returns all the files even in folders and then reports the files in the folders are missing. It would be nice to show the actual folder though and then navigate into the folder.

MaxHasADHD avatar Dec 24 '14 02:12 MaxHasADHD

I think this would be possible by adding the folder url to the query's searchScope. Not replacing NSMetadataQueryUbiquitousDocumentsScope, but adding the folder url additionally to it! But I'm not sure if this would even work with this library, because of the whole enumerateCloudDocuments logic. I'm also looking for folder support, because I'm struggling with my own implementation. iCloud is such a mess!

tobihagemann avatar Apr 10 '15 10:04 tobihagemann

Hmmm... scratch that! Using NSFileManager's contentsOfDirectoryAtURL method should be sufficient. :D It's just too weird using NSMetadataQuery.

tobihagemann avatar Apr 10 '15 11:04 tobihagemann

I found that by modifying the listCloudFiles to the following solved most of our problems:

- (NSArray *)listCloudFiles {
    // Log retrieval
    if (self.verboseLogging == YES) NSLog(@"[iCloud] Getting list of iCloud documents");

    // Check for iCloud
    if ([self quickCloudCheck] == NO) return nil;

    // Get the directory contents
    NSMutableArray *directoryContent = [[NSMutableArray alloc] init];

    [self appendContentsOfDirectoryAtURL: [self ubiquitousDocumentsDirectoryURL] contents: directoryContent];

    // Log retrieval
    if (self.verboseLogging == YES) NSLog(@"[iCloud] Retrieved list of iCloud documents");

    // Return the list of files
    return directoryContent;
}


- (void)appendContentsOfDirectoryAtURL: (NSURL*)directory contents: (NSMutableArray*)contents
{
    if (self.verboseLogging == YES) NSLog( @"[iCloud] appendContentsOfDirectoryAtURL: %@", directory.absoluteString );
    NSArray* directoryContents = [self.fileManager contentsOfDirectoryAtURL: directory
                                                 includingPropertiesForKeys: @[NSURLNameKey, NSURLIsDirectoryKey]
                                                                    options: 0
                                                                      error: nil];

    for (NSURL* fileURL in directoryContents)
    {
        NSNumber *isDirectory;
        [fileURL getResourceValue: &isDirectory forKey: NSURLIsDirectoryKey error: nil];
        if ([isDirectory boolValue])
        {
            [self appendContentsOfDirectoryAtURL: fileURL contents: contents];
        }
        else
        {
            [contents addObject: fileURL];
        }
    }
}

This recursively iterates through directories adding them to the list of cloud documents. The only other thing we had to do was ensure we created the directory before attempting to save a document to it.

marchbold avatar Aug 18 '16 00:08 marchbold