9

I added a applicationShouldOpenUntitledFile method to my application delegate, returning NO as Apple's documentation specifies. However, I'm still getting a new document on startup. What's wrong?

@implementation AppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSLog( @"This is being called" );
}

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    NSLog( @"This never is" );
    return NO;  
}

@end
Charles
  • 50,943
  • 13
  • 104
  • 142
Steven Fisher
  • 44,462
  • 20
  • 138
  • 192

5 Answers5

12

You're running Lion. When you ran before adding the applicationShouldOpenUntitledFile handler, a new document was created. Now, with 10.7's "Restore windows when quitting and re-opening apps", your application is restoring that untitled window, and not creating a new one as you suppose.

Close that window and re-run your application, and applicationShouldOpenUntitledFile will be called and will suppress the creation of a new untitled file.

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
  • Correct - this is actually specified in Lion's release notes. – Joshua Nozzi Sep 27 '11 at 17:18
  • But not in the documentation for `NSApplicationDelegate`, which is where I was looking. :) `NSApplicationDelegate` describes the old behaviour as if it still applies without any mention at all of the new behaviour. I imagine that'll get fixed in a future SDK. – Steven Fisher Sep 27 '11 at 17:57
  • The release notes *are* the documentation. Sure, something might not yet have been updated in the API reference, but you should *always* read the release notes for any OS updates. – Joshua Nozzi Sep 27 '11 at 20:28
  • Do you have a specific suggestion for improving this question/answer? I tried to add a link to Apple's documentation, but it's documented by implication rather than explicitly stated anywhere. – Steven Fisher Sep 27 '11 at 21:15
  • 1
    You mean *besides* pointing out where this information is mentioned in my first comment? No. – Joshua Nozzi Sep 27 '11 at 21:56
  • It's obviously bug. Because there is no way to know whether is restored document exist when application was launched. NSDocumentController doesn't contain document (will be restored) at this time. So We can't figure out what do we have to open untitled document or not. – jeeeyul Oct 04 '12 at 05:13
  • Don't implement `applicationShouldOpenUntitledFile`; OS X should do the rest automatically for you. – Steven Fisher Oct 04 '12 at 16:51
6
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
    // Schedule "Checking whether document exists." into next UI Loop.
    // Because document is not restored yet. 
    // So we don't know what do we have to create new one.
    // Opened document can be identified here. (double click document file)
    NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(openNewDocumentIfNeeded) object:nil];
    [[NSOperationQueue mainQueue] addOperation: op];
}

-(void)openNewDocumentIfNeeded
{
    NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];

    // Open an untitled document what if there is no document. (restored, opened).       
    if(documentCount == 0){
        [[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
    }
}
jeeeyul
  • 3,727
  • 1
  • 24
  • 37
2

I'm using Xcode 8.3.2 and compiling for Os X 10.11 using a storyboard for a document based app. I noted that, if you set the window controller as initial controller, a window is created without any document and without calling applicationShouldOpenUntitledFile.

I solved removing the "is initial controller" checkbox in the storyboard.

Sgorbyo
  • 43
  • 7
1

If you're not running Lion / 10.7 or later, this can still happen if you have some other window open (even a non-Document window) when applicationShouldOpenUntitledFileshould be called.

I have a Document-based app where the AppDelegate class opens a global logging window, both for debugging purposes and for user status messages. If I have the program display that window on startup while running on OS X 10.6, applicationShouldOpenUntitledFile never gets called, even with no document windows displayed. If I turn that window off, the call is made.

Adam Wilt
  • 553
  • 4
  • 10
1

Since OSX Lion, the app's state restoration may interfere with your custom preferences for this exercise.

Citing an update to Aaron Hillegass and Adam Preble's book Cocoa Programming for MacOSX:

Note that Mac OS X Lion's state-restoration features may make it tricky to observe the new document preference. You can disable state restoration by editing the Run scheme in Xcode. Open the product menu and select Edit Scheme. Select the Run RaiseMan.app scheme, change to the Options pane, and check Disable state restoration.

Fnord23
  • 325
  • 2
  • 18