3

On lion after each crash when I start the app Lion asks if I want it to restore the application windows since it did not terminate properly last time, how can I disable this?

BACKGROUND: I am developing a Cocoa app for Lion (and Snow Leopard), it is a scientific app (not a consumer app, and is used only in house, no distribution outside).
It interfaces with a couple of hardware and it crashes a lot! (I know I should make it not crash but there are lots of legacy C code involved and not well written drivers and ...).

QUESTION: On lion after each crash when I start the app Lion asks if I want it to restore the application windows, how can I disable this?

Ali
  • 18,665
  • 21
  • 103
  • 138

3 Answers3

6

This seems to work but there isn't anything documented about it, so it might not work in a future OS update:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ApplePersistenceIgnoreState"];

Preventing Lion from State Save

Jason Harwig
  • 43,743
  • 5
  • 43
  • 44
  • from http://developer.apple.com/library/mac/#releasenotes/Cocoa/AppKit.html#10_7StateRestoration "Ignoring Existing Restorable State (New since early 2011 seed) When the user default ApplePersistenceIgnoreState is defined, existing restorable state and Untitled documents are ignored. New restorable state and Untitled document autosaves are redirected to a temporary directory, whose path will be logged to the console. This user default is intended for automated tests that want to start with a clean environment, and for debugging." – Ali Mar 27 '12 at 14:55
2

You can subclass NSApplication and implement restoreWindowWithIdentifier:state:completionHandler: (see also Windows Are Restored Automatically in the Document-Based App Programming Guide for Mac; unlike its sibling method on NSWindows this one returns a BOOL). For example, add a property preventWindowRestoration to your NSApplication subclass, so you can do this:

- (BOOL)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler
{
    if ([self preventWindowRestoration]) return NO;

    return [super restoreWindowWithIdentifier:identifier state:state completionHandler:completionHandler];
}

You need to set your property in applicationWillFinishLaunching: at latest since the restoration is happening right between applicationWillFinishLaunching: and applicationDidFinishLaunching:. And don't forget to specify your NSApplication subclass as principal class in Info.plist.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • `-restoreWindowWithIdentifier:...` isn't always called in Yosemite, so it can't be reliably used to prevent restores unfortunately. – Dimitri Bouniol Jun 03 '15 at 21:42
1

None of the above answers worked for me (I didn't try the defaults key trick but it doesn't seem to be intended for production use). Here's what worked for me:

In Xcode, open the window xib of storyboard and select the NSWindow.

Show the Utilities panel (right side pop-in) and select the Attribute Inspector (looks like a slider knob) and uncheck '[ ] Restorable' and '[ ] Visible At Launch'.

The restorable property can be set for windows you create:

NSWindow* window = ...
window.restorable = NO;

I can't find the best time to set the window.visible property, but setting it in the xib work for me.

alfwatt
  • 2,010
  • 2
  • 18
  • 29