0
votes

I'm developing an document-based application that allow users to insert images, movies to make compositions.

When saving the document, I create a directoryFileWrapper in which I put the xml representation of my project, and one fileWrapper for each images/movies using the original path (if document is not saved yet) or the it's relative path inside the package.

Once it's saved, users can move the document to another location/mac and still have access to their content.

If user do a "SaveAsOperation", and save it at the same location, the document is first deleted, and then I have to recreate it. The problem is that I can't recreate the fileWrappers using the relative path, as the document itself is already deleted.

Is there a way to prevent it to happen? What's the right way to save existing files into packages?

1
Which method of NSDocument did you override? - Willeke
-fileWrapperOfType: error: - vtruant
-readFromFileWrapper:ofType:error: - vtruant
Those are for reading. Which method of NSDocument did you override for writing? Did you try -[NSDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:]? - Willeke
-fileWrapperOfType: error: is called during the write operation. I tried writeToURL, but the file is deleted before this call. - vtruant

1 Answers

0
votes

The way to do it is:

in .h

  • create an instance var NSFileWrapper.

In the init:

  • create an empty directoryFileWrapper.

Each time you want to add a file in the wrapper:

  • add this file inside the directoryFileWrapper

In -readFromFileWrapper:(NSFileWrapper *)inFileWrapper ofType:error:

  • make "inFileWrapper" your fileWrapper (don't forget to release previous and retain the new one)

In -fileWrapperOfType:error:

  • return you fileWrapper.

If you also want to use NSFileWrapper, this link may be helpful https://www.cocoanetics.com/2012/09/back-on-mac-os-x-tutorial-for-ios-developers/ and the github repo is https://github.com/Cocoanetics/Examples