3

我有这样的代码(我尝试从云打开文档):

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。这可能是什么原因?

4

2 回答 2

1

我不能确切地告诉你你需要做什么,但我可以告诉你如何得到错误信息,以便你弄清楚。

在您BCCardDocument班级的@implementation部分中,添加如下内容:

- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted {
    NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo);
    [super handleError:error userInteractionPermitted:userInteractionPermitted];
}
于 2012-05-16T02:39:53.480 回答
0

我有与此非常相似的代码,它工作正常。我假设您的 BCCardDocument 是 UIDocument 的子类?如果是这样,它需要有这两种方法:

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError

- (BOOL)loadFromContents:(id)contents
              ofType:(NSString *)typeName
               error:(NSError *__autoreleasing *)outError {

唯一的其他区别是我不调用 stopQuery。

于 2012-05-16T02:25:57.097 回答