Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac Catalyst applicationShouldTerminateAfterLastWindowClosed #10

Open
tommycarpi opened this issue Jan 27, 2021 · 4 comments
Open

Mac Catalyst applicationShouldTerminateAfterLastWindowClosed #10

tommycarpi opened this issue Jan 27, 2021 · 4 comments

Comments

@tommycarpi
Copy link

Hi guys,
I've got another question, with Dynamic on Mac Catalyst is it possible to include applicationShouldTerminateAfterLastWindowClosed in the AppDelegate?

#if targetEnvironment(macCatalyst)
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
// ...
}
#endif

Thanks in advance

@polymerchm
Copy link

polymerchm commented Jan 28, 2021

Not clear to me that method is exposed in ApplicationDelegate. Only in NSApplicationDelegate. Likely, you will need to create an observer for the notification, if you can figure out what the notification string is.

I leave this to Mhd.

@mman
Copy link

mman commented Jan 28, 2021

I have tried to set my custom NSApplicationDelegate using the loaded AppKit bundle trick to achieve this, but Catalyst did override/ignore my custom delegate, and it did not work. I'm also interested how to make this work.

@tommycarpi
Copy link
Author

I managed to subscribe to some notifications in order to be alerted upon resignation and activation.
I couldn't solve the ShouldTerminateAfterLastWindowClosed yet, but that's a start.

Here is the code for future reference:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    #if targetEnvironment(macCatalyst)
    NotificationCenter.default.addObserver(self, selector: #selector(resignActive), name: NSNotification.Name("NSApplicationDidResignActiveNotification"),object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(becomingActive), name: NSNotification.Name("NSApplicationDidBecomeActiveNotification"),object: nil)
    #endif
}

@objc func resignActive() {
    // ... your code
  }
  
@objc func becomingActive() {
    // ... your code
}

@mman
Copy link

mman commented Nov 13, 2023

More debugging here: If you hook into the app/scene lifecycle, you will see that NSApplication.shared.delegate is actually nil until first scene completes loading. Only after that it makes sense to actually swap the NSApplicationDelegate class.

However, Mac Catalyst app actually expects the NSApplicationDelegate to be of class UINSApplicationDelegate, whose dump can be found for example here: https://github.com/w0lfschild/macOS_headers/blob/master/macOS/PrivateFrameworks/UIKitMacHelper/61000/UINSApplicationDelegate.h and that delegate implements couple of undocumented methods and variables that you need to cope with.

No success so far on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants