iCloud Drive list directories and files through NSMetadataQuery

1.4k Views Asked by At

I have built an iCloud-enabled app named "rmc".My app now can upload files to iCloud Drive and get metadata by NSMetadataQuery.But NSMetadataQuery's results only include the files in my APP's Container.Please see this link:https://developer.apple.com/videos/wwdc/2015/?id=234, this is a document and it is said that NSMetadataQuery can get files in other APP's container.What I want is that I can get all files in iCloud Drive,just like the app "Document 5" and "Cloud Lite",they can get all files. enter image description here

please see this picture.It is the UI of APP "Document 5".This APP can get the root directory information of iCloud Drive.But my app only can get files in the folder of "rmc".I also want to get the all directories and files in iCloud Drive.Like "Document 5" did.I hope if someone can help me and understand my question,because my English is pour.Thanks!!! This is my code:

    - (BOOL)getFiles
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];

    self.query = [[NSMetadataQuery alloc]init];
    self.query.predicate = [NSPredicate predicateWithFormat:@"%K like '*'",NSMetadataItemFSNameKey]; // all
    self.query.searchScopes = [NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope,NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope,nil];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(getFilesFinished:)
     name:NSMetadataQueryDidFinishGatheringNotification
     object:nil];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(getFilesFinished:)
     name:NSMetadataQueryDidUpdateNotification
     object:nil];

    [self.query startQuery];
    return YES;
}

this method used NSMetadataQuery to get metadata from iCloudDrive.And I set the searchScopes = [NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope,NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope,nil];

This is Apple's Documentation about this value: NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope Search for documents outside the app’s container. This search can locate iCloud documents that the user previously opened using a document picker view controller. This lets your app access the documents again without requiring direct user interaction. The result’s NSMetadataItemURLKey attributes return security-scoped NSURLs. For more information on working with security-scoped URLs, see Security-Scoped URLs in NSURL Class Reference.

0

There are 0 best solutions below