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

Posted

技术标签:

【中文标题】正确处理在您的应用程序的另一个实例中使用 iCloud 删除 UIDocument【英文标题】:Correct handling of deletion of a UIDocument in another instance of your app with iCloud 【发布时间】:2013-03-14 07:51:46 【问题描述】:

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

- (void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *))completionHandler

这个方法被正确调用了,但我不知道在这个方法中做什么。 在我关闭文档的那一刻,如果它仍然打开,但看起来文档在关闭时保存在旧路径上,所以文档重新出现。

我已经进行了大量搜索,但在 Apple 文档和任何论坛中都没有找到任何内容。

有没有人有类似的经历或者有人正确处理了删除?

【问题讨论】:

看起来 Apple 的 Lister 示例应用程序只是关闭了视图控制器而不关闭文档。 【参考方案1】:

我发现,我关闭了文档 2 次,在第二次关闭它之前,我保存了它。 我删除了 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];
                       );
        ];

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

【讨论】:

感谢您发布此信息。 Apple 文档不太清楚UIDocument 是否会自行关闭,或者我应该在我的子类中这样做。顺便说一句,您可以收听UIDocumentStateChangedNotification 并检查documentState 是否包含UIDocumentStateClosed,而不是发布通知。要定义的通知少了一个:-)

以上是关于正确处理在您的应用程序的另一个实例中使用 iCloud 删除 UIDocument的主要内容,如果未能解决你的问题,请参考以下文章