1

I've found out, that I can detect the deletion of a UIDocument on the iCloud through following method:

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

This method gets correctly called, but I don't know what to do in the method. At the moment I close the document, if it's still open, but it looks like the document gets saved on the old path while closing, so the document reappears.

I have already searched intensely, but I haven't found anything neither in the Apple doc nor in any forum.

Has somebody made similar experience or has somebody handled the deletion correctly?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Linard Arquint
  • 574
  • 3
  • 12
  • 32

1 Answers1

3

I've found out, that I close the document 2 times and before closing it the 2nd time, I save it. I have remove the saveToURL method and now it works as expected.

For all who want to detect a deletion: Overwrite this method in your subclass of UIDocument with following code:

    - (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];
                       });
        }];
}

I hope this will help someone

Linard Arquint
  • 574
  • 3
  • 12
  • 32
  • 1
    Thanks for posting this. Apple docs were not too clear whether `UIDocument` would close itself or I should do that in my subclass. By the way, instead of posting a notification you can listen to `UIDocumentStateChangedNotification` and check if `documentState` contains `UIDocumentStateClosed`. One less notification to define :-) – bio Nov 16 '15 at 20:04