I have implemented State Restoration in my AppKit (Cocoa) app. That works.
However: If the user opens the app by double clicking a document file saved by the app, then I want the app not to restore the previous windows but only open the document that was opened in Finder.
The problem is that the state restoration appears to happen before the handlers for opening document files are invoked, so even if I had a way to suppress the state restoration, I have no knowledge of the pending openURL calls at that point, yet.
I understand that I could check the launch arguments, but that seems to be a rather dirty way. I'd think that I should be able to query the stored file paths/URLs from the app object somehow, but I can't find anything.
Or can I change the order of restoring documents vs. opening documents? I've so far found out that both happens deep inside [NSDocument restoreDocumentWindowWithIdentifier:state:completionHandler:]
, inside the completionHandler function. I could not find an overridable function below that where I'd have more control over this, though.
I implement State Restauration by overriding restoreStateWithCoder:
and encodeRestorableStateWithCoder:
in my NSDocument subclass.
Undocumented (= unsupported) feature: Restoring all documents when the app is launched by opening a document can be disabled. In applicationWillFinishLaunching
do
[NSUserDefaults.standardUserDefaults setBool:YES forKey:@"NSDiscardWindowsOnDocumentOpen"];