I would like to use the delegate methods of the UISceneSession
lifecycle to help inform my Mac Catalyst app when the user moves their focus away from the app (window) and then comes back to the app (window).
When the app first launches on Mac sceneWillEnterForeground
and sceneDidBecomeActive
are successfully called, however when I focus on another app then delegate methods such as sceneWillEnterForeground
are not called. Why?
If you check NSWindowDelegate.windowDidBecomeMain(_:)
you'll notice that its parameter is a notification whose name is NSWindowDidBecomeMainNotification
.
So, you can observe that notification to get notified whenever a window in your app becomes focused:
NotificationCenter.default.addObserver(forName: .init("NSWindowDidBecomeMainNotification"), object: nil, queue: nil) { notification in
print("This window became focused:", notification.object)
}
And you can also observe NSWindowDidResignMainNotification
to get notified when a window in your app lost focus.