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

时间:2011-10-24 14:22:37

标签: iphone objective-c ios5 nspredicate icloud

我想在iCloud的Mobile Documents目录(在〜/ Library / Mobile Documents下的Lion中找到的目录)中由用户或应用程序创建的所有目录的列表中读取。以下是此目录的示例:

iCloud's Mobile Documents

我尝试了以下代码,但我运行的查询将不包含任何代表我的文件夹的对象(使用NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey)。如果我对txt文件运行查询(使用@"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey),我将分别为txt文件返回5个对象。因此寻找txt文件是有效的,但不适用于目录。通过docs阅读,我注意到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);
    }

}

1 个答案:

答案 0 :(得分:0)

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