Ios 使用iCloud正确处理在应用程序的另一个实例中删除UIDocument

Ios 使用iCloud正确处理在应用程序的另一个实例中删除UIDocument,ios,cocoa-touch,icloud,uidocument,Ios,Cocoa Touch,Icloud,Uidocument,我发现,我可以通过以下方法检测iCloud上UIDocument的删除: - (void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *))completionHandler 这个方法被正确调用,但我不知道在这个方法中要做什么。 在我关闭文档的时候,如果文档仍然打开,但看起来文档在关闭时保存在旧路径上,因此文档会重新出现 我已经仔细搜索过了,但无论是在苹果文档还是任何论坛上,我都没有找到任何东西

我发现,我可以通过以下方法检测iCloud上UIDocument的删除:

- (void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *))completionHandler
这个方法被正确调用,但我不知道在这个方法中要做什么。 在我关闭文档的时候,如果文档仍然打开,但看起来文档在关闭时保存在旧路径上,因此文档会重新出现

我已经仔细搜索过了,但无论是在苹果文档还是任何论坛上,我都没有找到任何东西


是否有人有过类似的经历,或者是否有人正确处理了删除操作?

我发现,我关闭了文档两次,在第二次关闭之前,我保存了它。 我已经删除了saveToURL方法,现在它可以正常工作了

对于所有希望检测删除的用户: 用以下代码覆盖UIDocument子类中的此方法:

    - (void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *errorOrNil))completionHandler
{
    sceneLampDocument* presentedDocument = self;
    [presentedDocument closeWithCompletionHandler: ^(BOOL success) {
        NSError* error = nil;
        if (!success)
        {
            NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"Could not close document that is being deleted on another device",
                                      NSLocalizedDescriptionKey, nil];
            error = [NSError errorWithDomain: @"some_suitable_domain"
                                        code: 101
                                    userInfo: userInfo];
        }

        completionHandler(error);  // run the passed in completion handler (required)

        dispatch_async(dispatch_get_main_queue(), ^
                       {
                           //[super accommodatePresentedItemDeletionWithCompletionHandler:completionHandler];

                           NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self forKey:@"document"];
                           [[NSNotificationCenter defaultCenter] postNotificationName: @"documentDeletedOnAnotherDevice"
                                                                               object: self
                                                                             userInfo: userInfo];
                       });
        }];
}

我希望这会对某人有所帮助

谢谢你发布这篇文章。苹果文档不太清楚是
UIDocument
会自动关闭,还是我应该在我的子类中关闭。顺便说一下,您可以收听
UIDocumentStateChangedNotification
并检查
documentState
是否包含
UIDocumentStateClosed
,而不是发布通知。少了一个需要定义的通知:-)看起来苹果的Lister示例应用程序只是在不关闭文档的情况下关闭了视图控制器。