【问题标题】:iCloud: How to read in directories created by the useriCloud:如何读取用户创建的目录
【发布时间】:2011-10-24 14:22:36
【问题描述】:

我想阅读用户或应用程序在 iCloud 的移动文档目录(在 Lion 中的 ~/Library/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);
    }

}

【问题讨论】:

    标签: iphone objective-c ios5 nspredicate icloud


    【解决方案1】:

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

    【讨论】:

    • 你有没有解决过这个问题?值得一提的是,在 Mountain Lion 中,用户可以从应用程序内的“打开”面板创建文件夹。你试过NSMetadataQuery API吗?这应该给我们一个提示,这在 iOS 中是如何变化的......
    • @nacho4d 不,最后我没有使用文件夹,抱歉。正如你所说,这可能会随着 M Lion 而改变。如果您找到方法,请随时通知我。
    • 只是为了记录(因为我想你已经注意到了)我在这里发布了一个解决方法:stackoverflow.com/questions/7873974/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2020-01-23
    • 2014-08-23
    相关资源
    最近更新 更多