iCloud Drive list directories and files through NSMetadataQuery

回眸只為那壹抹淺笑 提交于 2019-12-10 18:27:57

问题


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.

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.

来源:https://stackoverflow.com/questions/31869644/icloud-drive-list-directories-and-files-through-nsmetadataquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!