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

标签 iphone objective-c ios5 nspredicate icloud

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

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);
    }

}

最佳答案

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

关于iphone - iCloud:如何读取用户创建的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7877118/

相关文章:

iphone - json 模式到 objective c - 模型类生成

iphone - 为什么我的 Objective-C 对象被释放?

iphone - 在应用程序卸载时保留 SQLite 数据

c# - iOS 5 中的 NSJSONSerialization 就像 c# 序列化到类

iphone - 在我的应用程序中打开 iBooks 中的文档

iphone - 实现照片应用程序的全屏栏消失行为

ios - NSString sizeWithFont forWidth 返回错误值

iphone - 适用于 iOS 5 的 MFMailComposeViewController : Tutorial or Example

iphone - 字体颜色疑惑

iphone - 我们可以在 iPhone 应用程序中打开 PowerPoint 文件吗?