Edit AllPages

As added comfort to the user you might want to offer the possibility of restoring all the documents that where open at the time of quitting at startup. This can be accomplished through various means. Here most of the work is done by the application controller, a delegate of the NSApplication instance, the defaults and the shared instance of the NSDocumentController. All the coding is done in the application controller

Interface Builder —- If you don’t have one for your document based application yet you need to create an empty class, drag the class into the MainMenu.nib file, and create an instance. Ctrl-Drag a connection from the “Files owner” to your instance and make it the delegat. The files owner here represents the NSApplication object.

The NSApplication delegate gets called for application wide events like startup, quitting, hide and show. It is also very helpful if you want to direct events from the Menu to the current document in document based applications.

NSUserDefaults —- To use the defaults system you should declare an identifier for your applikation (in PB target settings, under “Applikation Settings” if you don’t supply one the Name of the applikations is going to be used. Apple suggests that you use a java-like identifier (e.g.

The Code —-

@implementation PWAppController

//Define these so the compiler can catch spelling errors NSString *PWOpenDocListFlagKey = @”Open Documents Flag”; NSString *PWOpenDocListKey = @”Open Documents”;

//initialize gets called before any other call to the class //so the defaults are declared here (Hillebrand p. 161)

// This delegate message gets called whenever the application is quit // per apple docs this does NOT include poweroff and logoff events … // more to come

// After Launching reopen the saved files


– HaRald

Questions, Comments And Suggestions: —-

I’ve found one error with this code in applicationWillFinishLaunching:

// After Launching reopen the saved files

SJI - It should be:

    [[NSDocumentController sharedDocumentController]openDocumentWithContentsOfURL:
						[NSURL fileURLWithPath:fileName] 
						display:YES error:NULL];

SJI - This fails if the user double clicks a document to launch the app with. That document is opened along with the previous ones, even if they are the same document, thus leaving you with two copies of one document open.

Graham Perks: applicationShouldTerminate can’t be used. If there’s a dirty document, it is closed via the “Save this document” sheet before applicationShouldTerminate is called, so dirty docs never get re-opened. Instead, use

with the same content. Add [NSApp terminate:sender] to the end. Then go to IB and point the Quit menu at this terminate selector rather than the NSApplication one. Basically you’re getting the Quit menu event, saving the list of open documents, then calling what the Quit menu would normally invoke.

My (Pierre Bernard) solution involves a NSDocumentController subclass:

@interface DocumentController (Private)


@implementation DocumentController


In the application delegate: