9

I have a UIDocument named "Old". I've made some changes to it and would like to re-save it under the name "New". The contents of "Old" should not change. Here is my failed attempt:

First, copy the yet unsaved "Old" document to "New" file location. Save "Old" document to "New" file location. In theory, because the "Old" document was never saved to its URL in the file system, it should still be in its "Old" state. The "New" file location should have the recent changes to "Old" because we just saved the "Old" document to the "New" file location.

NSFileManager *manager = [NSFileManager defaultManager];
NSURL *newFileLocation = [documentDirectoryURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", document.title, DOCUMENT_EXTENSION]];
NSURL *oldFileLocationCopy = document.fileURL.copy;
NSError *error;
BOOL copySuccess = [manager copyItemAtURL:oldFileLocationCopy toURL:newFileLocation error:&error];
if (! copySuccess)
{
    NSLog(@"File not successfully copied.");
    return;
}
[document saveToURL:newFileLocation forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success)
 {
     if (success)
     {

         handler();
     }
     else
     {
         NSLog(@"New file rename not saved.");
     }
 }];

The result of this code is just a simple renaming operation. "Old" disappears, "New" has the contents of the newly saved document.

I feel like this should be easy, but I haven't found any similar questions on SO. Thanks in advance!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.