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 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.
请看这张图。这是APP“Document 5”的UI。这个APP可以获取iCloud Drive的根目录信息。但是我的应用程序只能获取“rmc”文件夹中的文件。我也想获取所有iCloud Drive中的目录和文件。就像“文档5”一样。我希望有人能帮助我并理解我的问题,因为我的英语很流利。谢谢!
这是我的代码:
- (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;
}
此方法使用 NSMetadataQuery 从 iCloudDrive 获取元数据。我设置了 searchScopes = [NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope,NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope,nil];
这是苹果关于这个值的文档:
NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope
搜索应用程序容器外部的文档。此搜索可以找到用户之前使用文档选择器视图控制器打开的 iCloud 文档。这使您的应用程序可以再次访问文档,而无需直接用户交互。结果的 NSMetadataItemURLKey 属性返回安全范围的 NSURL。有关使用安全范围 URL 的更多信息,请参阅 NSURL 类参考中的安全范围 URL。