How a macOS document-based app start itself?

806 Views Asked by At

I am learning the document-based app architecture of macOS app development, but am confused about it.

I created a document-based app in Xcode. The app template created a simple document-based app. It can run, and will create a new document automatically when it is run.

My question is: How does this app start the document architecture? I suppose that there should be some code like the following:

NSDocumentController *docController;
docController = [NSDocumentController sharedDocumentController];
[docController newDocument:self];

But I can't find any such code in the created app. The - (void)applicationDidFinishLaunching: method in the app delegate is empty. Also, there is not any NSDocumentController object in the main nib file.

So, how does this app know that it should use the document architecture, initialize a NSDocumentController, and then create a new document?

1

There are 1 best solutions below

0
On

how does this app know that it should use the document architecture, initialize a NSDocumentController, and then create a new document?

I’m unclear about the inner workings of Xcode, but it appears that in the app’s info.plist CocoaNSDocumentClass has to be set to Document and Role has to be set to Editor (or Viewer, None, or Shell) in order for the document to be created. In your demo try deleting either one or both of these lines in the plist and see if the app still works. Both settings are located on the second level of Document types. In my experience, deleting either of these lines breaks the app and restoring the correct setting fixes it. In a programmatically created document-based app (no nib) and compiling with Terminal the following errors are generated when I go inside the bundle and run the executable after deleting one or the other info.plist settings mentioned above.

2020-02-11 19:31:48.294 nsdoc_demo[1606:658926] NSDocumentController Info.plist warning: The values of CFBundleTypeRole entries must be 'Editor', 'Viewer', 'None', or 'Shell'.

2020-02-11 19:38:46.355 nsdoc_demo[1748:680819] -[NSWindowController loadWindow]: failed to load window nib file 'Document'.

Xcode (with Document.xib) generates the following error when CocoaNSDocumentClass is not set:

2020-02-11 16:22:03.022602-0600 docExpt[1166:428702] The DocumentType type doesn't map to any NSDocumentClass.

In my experience the source code can be ok, but if the info.plist is not set correctly a document-based app won’t work as expected.