Re: Is this a bug in AppDelegate?


Peter Teeson
 

Hi Bill. Thanks for your (Swift) reply <grin>. You may be correct in how the flow works.
Mind you I have not found any documentation to support it; can you point me to some?

I was basing my assumption on the Xcode Documentation and API reference.
(That’s why I put those comments in the skeleton method implementations)

Basically I inferred the classic truth table:
Should open NO  NO YES YES
OpenUntitled NO YES NO YES

Open Window NO NO NO YES Expected
Open Window NO NO YES NO Actual

These tests were all launched with Run option "Launch application without state restoration”.

 FWIW

Peter


On Aug 5, 2018, at 5:02 PM, Bill Pitcher <bill@...> wrote:

Hi Peter,

I think you have the flow here a little confused.

func applicationShouldOpenUntitledFile(_ sender: NSApplication) -> Bool
lets the App know if there is going to be an Untiled Document, false neither the App Default nor the applicationOpenUntitledFile will be called. (Watch out that it doesn’t load an already created one automatically.)

IF applicationShouldOpenUntitledFile returns TRUE

func applicationOpenUntitledFile(_ sender: NSApplication) -> Bool
This gives the delegate an opportunity to create an Untitled Document

Returning true from this tells the App that you have taken responsibility for creating the Untiled document and that it shouldn’t.
Return false at this point tells the App applicationOpenUntitledFile didn't create the Untiled Document and that it should make the default Untitled

That’s my take on it, hope it helps.
cheers
Bill Pitcher
Tutor
Literacy Aotearoa - Dunedin




On 6/08/2018, at 7:35 AM, Peter Teeson via Groups.Io <peter.teeson@...> wrote:

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.