2

Is there a manner to list all files (documents+data) I have in the iCloud (from a Mac) ? I believe that the NSMetadataQuery object can help me with that, but is there any sample code out there ?

Thanks !

Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89

1 Answers1

6

Here some sample code to do a query for txt files in your iCloud folder. If you want to look for other files, simple replace the predicate (NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey];).

To list all files, you could simply do @"NOT %K.pathExtension = '.'" but I'm not sure if this is the most elegant method. Suggestions welcome.

Have a look at this post to get the context and full code sample. Here is just the method to look for files.

-(void)loadDocument {

    // (2) iCloud query: Looks if there are txt files in the cloud

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    _query = query;
    //SCOPE
    [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
    //PREDICATE
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey];
    [query setPredicate:pred];
    //FINISHED?
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
    [query startQuery];

}
Community
  • 1
  • 1
n.evermind
  • 11,944
  • 19
  • 78
  • 122
  • Thanks for your code n.evermind. I will try to adapt it to work on the mac environment (no UIDocument for example). – Laurent Crivello Oct 27 '11 at 15:12
  • @LaurentCrivello Your are welcome. Don't forget to mark the answer as correct if you feel that it was correct. – n.evermind Oct 27 '11 at 17:30
  • I actually can't find any files (maybe because I didn't upload anything...). But my goal was to find the pictures of the PhotoStream this way (and I know there are pics). Can this work with your query ? Thanks. – Laurent Crivello Oct 29 '11 at 11:39
  • @LaurentCrivello I guess it might (if the photostream is accessible to other apps). The query as I have it right now is for the iCloud folder of your app (whether it is an iPhone/iPod/Mac app). If your looking for the photostream folder, it may be worth asking a separate question here on SO. – n.evermind Oct 30 '11 at 19:48