user3226859
user3226859

Reputation: 87

Cocoa How to remember the window position with multiple monitors?

To save window position for one(main) monitor i'm using this code:

[[win windowController] setShouldCascadeWindows:NO]; 
[win setFrameAutosaveName:@"My App"];

It's works fine. But if I connect second monitor and drag my app window to second monitor, cocoa autosave does not work - after restart, window always placed on primary(main) monitor. Any ideas? Thank you!

Upvotes: 0

Views: 1396

Answers (1)

Marek H
Marek H

Reputation: 5566

Enable restore for your app:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  [[NSUserDefaults standardUserDefaults] setObject:@YES forKey:@"NSQuitAlwaysKeepsWindows"];
}

Set your window to be restorable in Interface Builder

Also make sure that you don't call invalidate on close. It is just promise -> you might end app with deleted state

[self.window invalidateRestorableState];

To verify/debug check if the content of "~/Library/Saved Application State/yourbundleidentifier" persists after restart.

Upvotes: 6

Related Questions