Tôi muốn đọc trong danh sách tất cả các thư mục được tạo bởi người dùng hoặc ứng dụng trong thư mục Tài liệu di động của iCloud (thư mục được tìm thấy trong Lion dưới ~/Library/Tài liệu di động). Dưới đây là một ví dụ về cách thư mục này có thể trông giống như:iCloud: Cách đọc trong các thư mục do người dùng tạo
Tôi đã thử các mã sau đây, nhưng truy vấn tôi chạy sẽ không chứa bất kỳ đối tượng đại diện cho các thư mục của tôi (sử dụng NSPredicate predicateWithFormat:@"%K.pathExtension = ''", NSMetadataItemFSNameKey
). Nếu tôi chạy truy vấn cho các tệp txt (sử dụng @"%K ENDSWITH '.txt'", NSMetadataItemFSNameKey
), tôi sẽ nhận được 5 đối tượng được trả về cho các tệp txt tương ứng. Tìm kiếm các tập tin txt do đó hoạt động, nhưng không phải cho các thư mục. Đọc qua docs, tôi nhận thấy rằng Apple đề nghị sử dụng NSFileWrapper (File Packages) thay vì thư mục. ICloud không thể xử lý/phát hiện các thư mục được tạo bởi người dùng hay ứng dụng?
Đây là mã của tôi:
-(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);
}
}
Bạn đã bao giờ làm việc xung quanh vấn đề này chưa? Đối với những gì nó có giá trị, trong Mountain Lion người dùng có thể tạo thư mục từ bảng điều khiển mở bên trong ứng dụng. Bạn đã thử API 'NSMetadataQuery' trong đó chưa? Điều đó sẽ cho chúng ta một gợi ý làm thế nào điều này đang thay đổi trong iOS ... – nacho4d
@ nacho4d không, cuối cùng tôi đã không đi với các thư mục, xin lỗi. Như bạn nói, điều này có thể thay đổi với M Lion. Giữ cho tôi được đăng nếu bạn tìm thấy một cách. –
Chỉ cần cho hồ sơ (vì tôi nghĩ rằng bạn đã nhận thấy) Tôi đăng một công việc xung quanh đây: http://stackoverflow.com/questions/7873974/returning-a-list-of-directories-with-nsmetadataquery-and-nspredicate/10510752 # 10510752 – nacho4d