How can I prevent Lion from showing if you want to restore your windows after a crash

On Lion, after every crash when I launch the application, Lion asks if I want it to restore the application windows since it did not exit properly the 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 only used in the home, no distribution outside).
It interacts with multiple hardware and it crashes a lot! (I know I have to make it not crash, but there is a lot of legacy C code, not well-written drivers and ...).

QUESTION: On Lion, after every crash when I launch the application, Lion asks if I want it to restore application windows, how can I disable this?

+3


source to share


3 answers


It seems to work, but nothing is said about it, so it may not work in a future OS update:

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

      



Preventing Leo from Save State

+6


source


You can subclass NSApplication

and implement restoreWindowWithIdentifier:state:completionHandler:

(see also Windows auto-repair in Mac's Document Based Programming Guide , unlike its sibling method on NSWindows, this one returns 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 to applicationWillFinishLaunching:

at the latest, since the restoration happens right between applicationWillFinishLaunching:

and applicationDidFinishLaunching:

. And don't forget to list your NSApplication subclass as the main class in Info.plist.

+2


source


None of the above answers worked for me (I haven't tried the default key trick, but it doesn't seem to be intended to be used in production). Here's what worked for me:

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

Show the Utilities panel (right side) and select the Attributes Inspector (looks like a slider knob) and uncheck the [] Restore and [] Visible at Startup.

The property to restore can be set for newly created windows:

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

      

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

0


source







All Articles