NSApp:隐藏 main window 直到可以加载适当的 ViewController

NSApp: Hide main window until proper ViewController can be loaded

我有一个 MacOS 应用程序,它是一个已注册的自定义 URL 处理程序。 如果应用程序是通过 url 处理程序或常规 window 启动的,我试图让它显示特定的 NSViewControllerViewController 如果没有使用参数。

在我的 AppDelegate.swift 我有这个:

   func application(_ application: NSApplication, open urls: [URL]) {
        AppDelegate.externalCaller = true;

        NSApp.hide(self)
        let url: URL = urls[0];

        // Process the URL.
        let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true);
        let method = components?.host;

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            if (method == "DO_STH") {
                // do something with no window
                NSApplication.shared.windows.last?.close();
            } else if (method == "DO_STH_2") {
                // do something with no window
                NSApplication.shared.windows.last?.close();
            } else if (method == "PROCESS_STUFF") {
                        // Show window
                        DispatchQueue.main.async {
                            let mainStoryboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil);
                            let restoringViewController = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("restoringData")) as! RestoringViewController;

                            if let window = NSApp.mainWindow {
                                window.contentViewController = restoringViewController;
                            }
                            NSApp.activate(ignoringOtherApps: true);
                            AppDelegate.restoreData();
                        }
                    }
            }
        }
    }

问题是在 NSApp.hide(self) 运行时,已经有一个 window 可见,默认为 viewController,产生了一些闪烁效果。

使应用程序在默认情况下启动时没有任何可见 window 并且仅在未使用任何 URL 参数启动时才显示 window 的最佳方法是什么?要求是否存在特定的 URL 参数?

取消选中 "Is initial Controller" 并将其添加到 AppDelegate 解决了我的问题

    func applicationDidFinishLaunching(_ notification: Notification) {
        if (!AppDelegate.externalCaller) {
            showWindow();
        }
    }

    func showWindow() {
        let mainStoryboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil);
        let windowController = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("MainWindowController")) as! NSWindowController;
        windowController.showWindow(self);
    }