UIDocument“另存为”功能

riz*_*zes 5 copy rename nsfilemanager ios uidocument

我有一个UIDocument名字叫“老”。我对其进行了一些更改,并希望以“ New”的名称重新保存它。“旧”的内容不应更改。这是我失败的尝试:

首先,将尚未保存的“旧”文档复制到“新”文件位置。将“旧”文档保存到“新”文件位置。从理论上讲,由于“旧”文档从未保存到文件系统中的URL,因此它仍应处于“旧”状态。“新”文件位置应将“旧”文件的最新更改,因为我们只是将“旧”文件保存到“新”文件位置。

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.");
     }
 }];
Run Code Online (Sandbox Code Playgroud)

该代码的结果只是一个简单的重命名操作。“旧”消失,“新”具有新保存的文档的内容。

我觉得这应该很容易,但是我还没有在SO上找到任何类似的问题。提前致谢!