App Launches on Login but Window Doesn't Appear Automatically (macOS Sequoia 15.2, Xcode 16.2)

I am developing a macOS app using SwiftUI, and I am encountering an issue when launching the app at login. The app starts as expected, but the window does not appear automatically. Instead, it remains in the Dock, and the user must manually click the app icon to make the window appear.

Additionally, I noticed that the timestamp obtained during the app's initialization (init) differs from the timestamp obtained in .onAppear. This suggests that .onAppear does not trigger until the user interacts with the app. However, I want .onAppear to execute automatically upon login.

Steps to Reproduce

  1. Build the app and add it to System Settings > General > Login Items as an item that opens at login.
  2. Quit the app and restart the Mac.
  3. Log in to macOS.
  4. Observe that the app starts and appears in the Dock but does not create a window.
  5. Click the app icon in the Dock, and only then does the window appear.

Expected Behavior

  • The window should be created and appear automatically upon login without requiring user interaction.
  • .onAppear should execute immediately when the app starts at login.

Observed Behavior

  • The app launches and is present in the Dock, but the window does not appear.
  • .onAppear does not execute until the user manually clicks the app icon.
  • A discrepancy exists between the timestamps obtained in init and .onAppear.

Sample Code

Here is a minimal example that reproduces the issue:

LoginTestApp.swift

import SwiftUI
@main
struct LoginTestApp: App {
    @State var date2: Date

    init(){
        date2 = Date()
    }

    var body: some Scene {
        WindowGroup {
            MainView(date2: $date2)
        }
    }
}

MainView.swift

import SwiftUI
struct MainView: View {
    @State var date1: Date?
    @Binding var date2: Date

    var body: some View {
        Text("This is MainView")
        Text("MainView created: \(date1?.description ?? "")")
            .onAppear {
                date1 = Date()
            }
        Text("App initialized: \(date2.description)")
    }
}

Test Environment

  • Book Pro 13-inch, M1, 2020
  • macOS Sequoia 15.2
  • Xcode 16.2

Questions

  • Is this expected behavior in macOS Sequoia 15.2?
  • How can I ensure that .onAppear executes automatically upon login?
  • Is there an alternative approach to ensure the window is displayed without user interaction?

Is this expected behavior in macOS Sequoia 15.2?

I haven't observed any behavior change here. The user has to bring your app from the background to the foreground.

How can I ensure that .onAppear executes automatically upon login?

To clarify, onAppear action is not based on view appearance. onAppear/onDisappear are about view lifecycle, an not about view visibility.

The exact moment that SwiftUI calls this method depends on the specific view type that you apply it to, but the action closure is completes before the first rendered frame appears.

You're better off using task(id:priority:_:) , which performs the asynchronous task before the view appears.

I initially asked for an explanation because I observed a different behavior in my environment. However, considering your response, I decided to implement the feature in a different way.

Thank you for your response.

App Launches on Login but Window Doesn't Appear Automatically (macOS Sequoia 15.2, Xcode 16.2)
 
 
Q