我有这样的代码(我尝试从云中打开文档):
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。可能是什么原因?
答案 0 :(得分:1)
我无法确切地告诉你你需要做什么,但我可以告诉你如何得到错误信息,以便你能够弄明白。
在BCCardDocument
班级@implementation
部分,添加以下内容:
- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted {
NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo);
[super handleError:error userInteractionPermitted:userInteractionPermitted];
}
答案 1 :(得分:0)
我有非常相似的代码,它工作正常。我假设您的BCCardDocument是UIDocument的子类?如果是这样,它需要有这两种方法:
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
- (BOOL)loadFromContents:(id)contents
ofType:(NSString *)typeName
error:(NSError *__autoreleasing *)outError {
唯一的另一个区别是我没有调用stopQuery。