Steven Fisher
Steven Fisher

Reputation: 44856

Why isn't applicationShouldOpenUntitledFile being called?

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

Upvotes: 10

Views: 2242

Answers (5)

Sgorbyo
Sgorbyo

Reputation: 43

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.

Upvotes: 2

Fnord23
Fnord23

Reputation: 325

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.

Upvotes: 1

jeeeyul
jeeeyul

Reputation: 3787

-(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];
    }
}

Upvotes: 7

Adam Wilt
Adam Wilt

Reputation: 583

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.

Upvotes: 1

Steven Fisher
Steven Fisher

Reputation: 44856

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.

Upvotes: 12

Related Questions