我有这样的代码(我尝试从云中打开文档):
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K ENDSWITH '.card'", NSMetadataItemFSNameKey];
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(queryDidFinishGathering:)
name:NSMetadataQueryDidFinishGatheringNotification
object:query];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(queryDidStartGathering:)
name:NSMetadataQueryDidStartGatheringNotification
object:query];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(queryDidUpdate:)
name:NSMetadataQueryDidUpdateNotification
object:query];
[query startQuery];
// ========================
- (void)queryDidFinishGathering:(NSNotification *)notification {
NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification
object:query];
for (NSMetadataItem* item in [query results]) {
NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
BCCardDocument *doc = [[[BCCardDocument alloc] initWithFileURL:url] autorelease];
[doc openWithCompletionHandler:^(BOOL success) {
if (success) {
NSLog(@"%@", doc.card.number);
}
}];
}
}
但是openWithCompletionHandler完成块的成功参数始终等于NO。可能是什么原因呢?
最佳答案
我无法确切地告诉您您需要做什么,但是我可以告诉您如何获取错误消息,以便您找出问题所在。
在BCCardDocument
类的@implementation
部分中,添加以下内容:
- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted {
NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo);
[super handleError:error userInteractionPermitted:userInteractionPermitted];
}