swiftmacosmac-catalystuikitformac

Detect application minimize event for UIKIt for Mac?


When i minimize the application applicationDidEnterInBackground not called for Mac Catalyst. After some search i found that Background delegate methods not called for Mac Catalyst but foreground method of Scene delegate called.

is there any way to track or handle minimize delegate methods like NSWindowDelegate have?

Thanks


Solution

  • Well, if you check NSWindowDelegate.windowWillMiniaturize: you'll notice that its parameter is just a notification (its name is NSWindowWillMiniaturizeNotification).

    So, you can simply observe that notification to get notified whenever a window in your app is about to be minimized:

    NotificationCenter.default.addObserver(forName: NSWindow.willMiniaturizeNotification, object: nil, queue: nil) { notification in
        print("This window is about to be minimized:", notification.object)
    }
    

    You can also do the same to get notified about other events like:

    Full list of notifications here.