Does NSDocument contain a complete folder?

I apologize if this argument has already been considered, but after some research I have not found anything concrete.

I need to create an application based on a document, where the document is not actually a single file, but a structured set of files in a directory. The window will show the pdf file contained in the folder with a specific file name and enrich it with information from other files in the folder. I can not use pdf annotations to achieve this, I really need to save the files separately from pdf.

What is the best approach to achieve this? All sample code I found uses a single file.

+8
directory objective-c folder cocoa nsdocument
source share
1 answer

You can use NSFileWrapper as the package directory in document-based applications.

In your application Info.plist file, indicate that your document type is a package or package ( LSTypeIsPackage key with a value of YES ).

In your NSDocument subclass NSDocument implement the following read and write methods. In this example, Im accepts the appropriate model instance variables: pdfData and signatureBitmapData , which are stored in the package directory in MainDocument.pdf and SignatureBitmap.png, respectively.

 - (BOOL)readFromFileWrapper:(NSFileWrapper *)dirWrapper ofType:(NSString *)typeName error:(NSError **)outError { NSFileWrapper *wrapper; NSData *data; wrapper = [[dirWrapper fileWrappers] objectForKey:@"MainDocument.pdf"]; data = [wrapper regularFileContents]; self.pdfData = data; wrapper = [[dirWrapper fileWrappers] objectForKey:@"SignatureBitmap.png"]; data = [wrapper regularFileContents]; self.signatureBitmapData = data; … return YES; } - (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError **)outError { NSFileWrapper *dirWrapper = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil] autorelease]; [dirWrapper addRegularFileWithContents:self.pdfData preferredFilename:@"MainDocument.pdf"]; [dirWrapper addRegularFileWithContents:self.signatureBitmapData preferredFilename:@"SignatureBitmap.png"]; … return dirWrapper; } 

From the user's point of view, the package directory is displayed in Finder, as if it were a single file, very similar to the Xcode.xcodeproj directories or application packages.

+12
source share

All Articles