Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Xcode setup a document based application?

I am learning Cocoa and my understanding from reading the documentation is that when an application starts the following happens:

  1. A shared application instance is created.
  2. The main nib file is looked up from the applications property list so that the application knows which nib to load.
  3. the run loop is started.

This is fine and makes sense for s single windowed application however I am confused by what xcode does when a document based application is created.

In this case there are two nib files; the first contains the application menu and the second contains the window which represents the NSDocument subclass. when I run the application a new document window is opened automatically.

Based on my understanding of how the application works outlined above I don't understand how my application knows to open the document window once the menu nib has been looked up from the property list. There is no code generated to do this as far as I can see (except for the windowNibName method but where is this called from?)

Can anyone tell me what xcode does differently so that the application knows that it is document based and therefore needs to open a document window?

Update:

What I am trying to understand is how Xcode knows how to do something different if my application is set up as a document based application rather than a single window application. As far as I am aware there is no setting to specify this and Xcode doesn't appear to generate any code to give this different behaviour.

From reading the documents over the last couple of days I think I know how this works but am not sure:

  1. _NSApplication_has a delegate method applicationOpensUntitledFile which is called by the applications delegate.
  2. NSDocumentController is set as the applications delegate by default and the default implementation looks for the presence of the CFBundledTypeInfo to determine if the document is document based or not and responds as is appropriate for the application (I.E. YES for document based application and NO for single window applications).
  3. The majority of the time when a single window application is created the application delegate is replaced by a custom AppController anyway which usually wont contain a definition for the applicationOpenUntitledFile method as it is not appropriate for the type of application.

Hopefully any Cocoa experts can confirm if my understanding is correct or if I am barking up the wrong tree.

like image 577
Benjamin Gale Avatar asked Jun 26 '11 19:06

Benjamin Gale


People also ask

What is document based app in Xcode?

Boost user productivity and provide an excellent user experience by incorporating key Apple technologies, such as UIDocument, Open in Place, Document Provider Extension, and iCloud Drive, into your document-based app.

What are document based apps?

A document-based app is just going to be an app which manages a list of these documents and presents them to the users so that they can view them or edit them or rename them. Keynote, for example, manages a list of Keynote presentation documents.


1 Answers

When you create a document-based application, you get a few things:

  • A subclass of NSDocument
  • A new xib file for this document, in addition to MainMenu.xib
  • A CFBundleDocumentTypes entry in Info.plist, which tells the app about your NSDocument subclass

When your app opens, the shared NSDocumentController will create a new untitled document using the CFBundleDocumentTypes information.

For more information, read The Document-Based Application Project Template and the rest of the document-based applications guide.

like image 134
jtbandes Avatar answered Oct 03 '22 02:10

jtbandes