iCloud:如何读取用户创建的目录

n.e*_*ind 6 iphone objective-c nspredicate ios5 icloud

我想在iCloud的Mobile Documents目录(在〜/ 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

n.e*_*ind 0

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

  • 只是为了记录(因为我认为你已经注意到)我在这里发布了一个解决方法:http://stackoverflow.com/questions/7873974/returning-a-list-of-directories-with-nsmetadataquery-and-nspredicate/ 10510752#10510752 (2认同)