Search code examples
scenemac-catalyst

Mac Catalyst - prevent scene creation or create with invisible window


I have Mac Catalyst app with 2 different scene configurations. I want app to always start with default scene only; the other will be created by code if needed. Problem is that session restoration system automatically restores scenes from previous session even if I don't want to. Is there a way to prevent a scene from auto-recreating?

I am aware that I can call UIApplication.shared.requestSceneSessionDestruction() directly from scene(willConnectTo: ...) of my SceneDelegate. However in this way the scene is first fully created, and only destroyed after that, which results in visible "flash" of empty window. Is it possible to prevent this flash? At least create an invisible window, so that scene will be created but never shown before scene destruction?


Solution

  • I solved it by setting this UserDefault key NSQuitAlwaysKeepsWindows in application(didFinishLaunchingWithOptions: ... ) in AppDelegate

    UserDefaults.standard.set(false, forKey: "NSQuitAlwaysKeepsWindows")
    

    This forces scenes to always closed when quitting app, so when starting, there are no scenes to restore.