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

发布于 2024-12-11 18:08:25 字数 2381 浏览 0 评论 0 原文

我想读取由用户或应用程序在 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);
    }

}

I would like to read in a list of all directories that are created either by the user or the app in iCloud's Mobile Documents directory (the one found in Lion under ~/Library/Mobile Documents). Here is an example of how this directory could look like:

iCloud's Mobile Documents

I tried the following code, but the query I run will not contain any objects representing my folders (using the NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey). If I run a query for txt files (using @"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey), I will get 5 objects returned for the txt files respectively. Looking for txt files thus works, but not for directories. Reading through the docs, I noticed that Apple suggests to use NSFileWrapper (File Packages) instead of directories. Is iCloud not able to handle/detect directories created by the user or the app?

Here is my code:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-12-18 18:08:25

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

I had a look at "Manage Storage" in the iClouds Settings in Mac OS X Lion. When I click my application, it will only show the different txt files (plus conflicting versions) and no directories whatsoever. So I have to assume that you can only work with wrappers / file packages but not with directories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文