2

I have a macOS app that needs to do some cleanups when the app looses focus (goes to the background), and some reloading of stuff when the it gains focus again (the app is now back in the foreground).

I have tried this code, both on the views and on the main app scene:

struct ContentView: View {
    @Environment(\.scenePhase) var scenePhase

    var body: some View {
        Text("Hello, world!")
            .padding()
            .onChange(of: scenePhase) { newPhase in
                if newPhase == .active {
                    print("Active")
                } else if newPhase == .inactive {
                    print("Inactive")
                } else if newPhase == .background {
                    print("Background")
                }
            }
    }
}

But I only receive an active state. Nothing else. I have also tried:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { (_) in
          print("UIApplication: active")
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { (_) in
          print("UIApplication: background")
}

But I can't get it to work.

How can I detect when the application goes to the background, and then the user brings it back to the front to work on it?

EDIT:

I created an appdelegate and it triggers, but only sometimes

class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidBecomeActive(_ aNotification: Notification) {
        print(">> app coming back, reloading data...")
    }

}
Aleph
  • 465
  • 2
  • 12

2 Answers2

1

this always worked for me:

@main
struct MyApp: App {

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        
        .onChange(of: scenePhase) { phase in
            if phase == .inactive || phase == .background {
                // save
            }
        }
    }
}
ChrisR
  • 9,523
  • 1
  • 8
  • 26
  • Thank you, but as I mentioned in the question, this doesn't get called. Neither in the `struct App` nor on any of the `views`. – Aleph Feb 11 '22 at 16:10
  • Sorry, I overread macOS! This could help: https://stackoverflow.com/questions/64992120/swiftui-and-appkit-how-to-know-if-the-window-is-focused – ChrisR Feb 11 '22 at 16:33
  • Yeah, code is very similar to the one I posted. It does work, but there seems to be an inconsistent detection of `applicationDidBecomeActive`. I'm still trying to debug that. – Aleph Feb 11 '22 at 17:02
1

The original code I wrote worked:

.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification))

Added this as to the view:

struct ContentView: View {

        var body: some View {
            Text("Hello, world!")
                .padding()
                .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) {
                     print("coming back!")
                 }
    }

But I had the project being Multiplatform and it was creating some issues. I turned the project into a macOS only project and it worked.

Aleph
  • 465
  • 2
  • 12