简体   繁体   中英

NSApp: Hide main window until proper ViewController can be loaded

I have a MacOS app that is a registered custom URL handler. I'm trying to make it show a specific NSViewController if the app is started via the url handler or the regular window and ViewController if no parameters are used.

In my AppDelegate.swift I have this:

   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();
                        }
                    }
            }
        }
    }

The problem is that by the time NSApp.hide(self) runs, there's already a window visible with the default viewController, creating some flickering effect.

What's the best way to make the app start without any visible window by default and only show the window if it wasn't started with any URL parameter and later on demand if a specific URL parameter exists?

Unchecking "Is initial Controller" and adding this to AppDelegate solved my issue

    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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM