I’m building a macOS document based app using SwiftUI’s DocumentGroup API. By default, when a document based app launches, macOS automatically shows a file open panel or creates a new untitled document window.
However, I want to suppress this default behavior and instead show a custom welcome window when the app starts — something similar to how Xcode or Final Cut Pro shows a “Welcome” or “Start Project” screen first.
So basically, when the user opens the app normally, it should not open the document selector or create a document automatically. Instead, it should show my custom SwiftUI or AppKit window. Here is my Code :-
//MyApp.swift
import SwiftUI
import AppKit
@main
struct PhiaApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
DocumentGroup(newDocument: MyDocumentModel()) { file in
EditorView(document: file.document, filePath: file.fileURL)
}
Settings { EmptyView() }
}
}
Current I have this code setup for my MainApp.swift, where I am using the AppDelegate to create a custom recording window using appkit and also defining the DocumentGroup to handle the custom .myapp
file opens. However, when I launch the app, its showing my appkit window as well as the macOs native file Selector to select the file I want to open.
I want when the user opens the app normally, it should not open the document selector or create a document automatically. Instead, it should show my custom SwiftUI or AppKit window. However, the app should still fully support opening .myapp
documents by double clicking from Finder, using the standard File → Open and File → New menu options, also having multiple document windows open at once.
This is my AppDelegate.swift file :-
import AppKit
import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate {
var panel: Panel?
private var statusItem: NSStatusItem?
func applicationDidFinishLaunching(_ notification: Notification) {
showWindow()
}
// MARK: - Window control
func showWindow() {
if panel == nil {
let root = RecordingViewMain()
let newPanel = Panel(rootView: root)
if let screen = NSScreen.main {
let size = NSSize(width: 360, height: 240)
let origin = NSPoint(
x: screen.visibleFrame.midX - size.width / 2,
y: screen.visibleFrame.midY - size.height / 2
)
newPanel.setFrame(NSRect(origin: origin, size: size), display: true)
}
panel = newPanel
}
panel?.makeKeyAndOrderFront(nil)
}
func hideWindow() {
panel?.orderOut(nil)
}
@objc private func showPanelAction() {
showWindow()
}
@objc private func quitAction() {
NSApp.terminate(nil)
}
}