8

I have checked the restorable option on my NSWindow When I move my application and change its size and close/reopen my application it sets the window size and position to the last size and position

but this doesn't happen on every computer where i test it. it only happens to a few computers

they don't have special settings regarding the resume.

Does anybody have any experience with this?

Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91

3 Answers3

26

But this doesn't happen on every computer where I test it. It only happens to a few computers. They don't have special settings regarding the resume.

Actually, they do. Take a look into the System Preferences > General pane. There should be an option named “Close windows when quitting an application” which is responsible for the restoration behavior. Additionally, in OS X Mountain Lion, Apple changed default behavior and now the app restores its state only when you quit using Command-Option-Q.

So, you probably should check which OS X version is installed on another Mac, and which preference is selected in the General pane. Hope this helps!

Vadim
  • 9,383
  • 7
  • 36
  • 58
  • 2
    I really wish there was a way for us to check the state of that checkbox. – Keith Smiley Apr 12 '13 at 17:57
  • Great answer! Couldn't figure out why it wouldn't restore until this – Jonathan Brown May 26 '16 at 17:25
  • 1
    Actually, we _can_ query the state of this checkbox: `[[NSUserDefaults standardUserDefaults] boolForKey:@"NSQuitAlwaysKeepsWindows"]` or `UserDefaults.standard.bool(forKey: "NSQuitAlwaysKeepsWindows")` gets the _inverse_ of the checkbox. That is, if the box is unchecked, the value in the user defaults is `true`. – DarkDust Nov 23 '17 at 13:50
  • None of this is valid anymore. – Cliff Ribaudo Sep 26 '19 at 09:45
5

One thing you should know is that checking the "Restorable" option in IB only changes a window property. The actual restoring and saving is in your hands.

First, you have to conform to the NSWindowDelegate protocol and implement the -window:willEncodeRestorableState:state and -window:didDecodeRestorableState: methods that encode and decode your window properties (For example your windows frame, which you obviously get by calling [myWindow frame]).

You also need to conform to the NSWindowRestoration protocol and implement +restoreWindowWithIdentifier:state:completionHandler:. (make sure you set your class to restoration class, with the setRestorationClass method)

For additional, more in depth, information, you can visit this Apple Documentation document here.

rien333
  • 1,154
  • 13
  • 15
  • I did that, and on my computer its working. i have placed a log statement in that method and it logs perfectly, but on my partner's computer ( who isn't a developer (i created an debug build)) it isn't working and the log statement doesn't show up.. – Andy Jacobs Oct 11 '12 at 09:22
3

If all you want is to restore the window's frame use setFrameAutosaveName:.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/WinPanel/Tasks/SavingWindowPosition.html

George
  • 4,189
  • 2
  • 24
  • 23