iCloud の Mobile Documents ディレクトリ (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);
}
}