【问题标题】:openWithCompletionHandler completion handler argument always = NOopenWithCompletionHandler 完成处理程序参数始终 = NO
【发布时间】:2012-01-06 10:43:09
【问题描述】:

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

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完成块的success参数总是等于NO。可能是什么原因?

【问题讨论】:

    标签: iphone ios ios5 icloud icloud-api


    【解决方案1】:

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

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

    唯一的不同是我不调用 stopQuery。

    【讨论】:

      【解决方案2】:

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

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

      - (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted {
          NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo);
          [super handleError:error userInteractionPermitted:userInteractionPermitted];
      }
      

      【讨论】:

        猜你喜欢
        • 2014-10-06
        • 1970-01-01
        • 2012-05-29
        • 2018-07-25
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        • 2015-08-30
        • 1970-01-01
        相关资源
        最近更新 更多