6

我想阅读用户或应用程序在 iCloud 的移动文档目录(在 ~/Library/Mobile Documents 下的 Lion 中找到的目录)中创建的所有目录的列表。这是该目录的外观示例:

iCloud 的移动文档

我尝试了以下代码,但我运行的查询将不包含任何代表我的文件夹的对象(使用 NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey)。如果我对 txt 文件运行查询(使用@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey),我将分别为 txt 文件返回 5 个对象。因此,寻找 txt 文件有效,但不适用于目录。通读文档,我注意到 Apple 建议使用 NSFileWrapper(文件包)而不是目录。iCloud 是否无法处理/检测用户或应用程序创建的目录?

这是我的代码:

-(void)loadDocument {

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    _query = query;
    //Search all files in the Documents directories of the application’s iCloud container directories:
    [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]]; 

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey];

    [query setPredicate:pred];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];
    [query startQuery];
}

- (void)queryDidFinishGathering:(NSNotification *)notification {

    NSMetadataQuery *query = [notification object];
    [query disableUpdates]; // You should invoke this method before iterating over query results that could change due to live updates.
    [query stopQuery]; // You would call this function to stop a query that is generating too many results to be useful but still want to access the available results.

    [self loadData:query];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];
    _query = nil; // we're done with it
}


- (void)loadData:(NSMetadataQuery *)query {

   NSLog(@"Query count %i", [query resultCount]);

    for (int i=0; i < [query resultCount]; i++) {
        NSMetadataItem *item = [query resultAtIndex:i];
        NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
        NSLog(@"%i.URL: %@", i, url);
    }

}
4

1 回答 1

0

我在 Mac OS X Lion 的 iCloud 设置中查看了“管理存储”。当我单击我的应用程序时,它只会显示不同的 txt 文件(加上冲突的版本),而不会显示任何目录。所以我不得不假设你只能使用包装器/文件包,但不能使用目录。

于 2011-10-24T16:50:04.400 回答