Re: Is this a bug in AppDelegate?


Peter Teeson
 

I can’t swear to it, but I’m pretty certain this is a result of state restoration
Good idea… However I just did this:
1. created a new document project
2. added to AppDelegate.m
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
    // Use this method to decide whether the application should open a new, untitled file.
    // Note that applicationOpenUntitledFile: is invoked if this method returns YES.
    
    return YES// YES if the application should open a new untitled file or NO if it should not.
}

- (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication {
    // Sent directly by theApplication to the delegate to request that a new, untitled file be opened.
    
    return NO// YES if the file was successfully opened or NO if it was not.
}
3. compiled and ran it with Run option "Launch application without state restoration”. 
Same result. There never was a previously saved untitled window.
But an untitled window was created even though I returned NO in applicationOpenUntitledFile.
Debugger showed:

2018-08-05 15:24:51.511 TestBug[12185:1447476] ApplePersistenceIgnoreState: 
Existing state will not be touched. 
New state will be written to /var/folders/w1/081y692x5x30c39mcr8lxwkh0000gn/T/phtSW.TestBug.savedState

What else could it be? 

On Aug 5, 2018, at 2:10 AM, Quincey Morris <quinceymorris@...> wrote:

On Aug 4, 2018, at 22:30 , Peter Teeson via Groups.Io <peter.teeson@...> wrote:

I expected that the app would not open an untitled file because
applicationOpenUntitledFile returned NO. 
But an untitled file was in fact opened in a window.

I can’t swear to it, but I’m pretty certain this is a result of state restoration. Assuming you had at some previous point created an untitled window, then state restoration will recreate it at next launch, without going through applicationOpenUntitledFile — because it’s not really creating a new document, but re-opening an autosaved one, which just happens to be called “Untitled”.

Try running it with the scheme option to prevent state restoration, and I suspect you’ll see the expected behavior.


Join cocoa@apple-dev.groups.io to automatically receive all group messages.