问题
I'm developing an app by using macCatalyst.
When user has closed the window on mac version, the app is terminated.
It was possible to change this behavior in cocoa by using:
applicationShouldTerminateAfterLastWindowClosed
method or by setting NSSupportsAutomaticTermination to NO in plist file.
How can I get the same in a macCatalyst app?
回答1:
I've discussed the problem with Apple technical developer support. After a long discussion, they accepted that this is a bug on Apple side. It's escalated to the engineering team.
UPDATE: Apple Engineering team has provided the following information regarding this issue and it worked for me:
We were able to prevent the quitting behavior by adding NSSupportsAutomaticTermination = NO to info.plist
回答2:
EDIT
This answer is not working, see the comments.
EDIT END
This is only possible, if you activate "Supports multiple windows":
My thoughts, why it is like this: macOS differentiates between applications and windows. So if your application can open only one window, closing that will tell macOS to close the application.
PS: I experienced some bugs in the "supports multiple windows" mode. I hope, that they will be removed soon.
回答3:
Here is my solution, maybe someone is still looking for this :)
1st we need to have access to AppKit. In this link you can find a good explanation of how to do it, as well as some sneak peek on the next step. link
Once we have an AppKit bundle ready, and read the explanation of how to disable the zoom button on the window (on the link above), we are ready to do what we actually want; which is to avoid the app termination when the Close button is clicked. For that we will simply hijack the Close button and tell it to do what we need...
- (void) tweakWindowButtons
{
NSArray *windows = NSApplication.sharedApplication.windows;
NSWindowCollectionBehavior behavior = NSWindowCollectionBehaviorFullScreenAuxiliary | NSWindowCollectionBehaviorFullScreenNone;
for (NSWindow *window in windows) {
[window setCollectionBehavior:behavior];
//-- Hijack close button action
NSButton *button = [window standardWindowButton:NSWindowCloseButton];
[button setTarget:self];
[button setAction:@selector(closeButtonAction)];
button = [window standardWindowButton:NSWindowZoomButton];
[button setEnabled:NO];
}
}
- (void) closeButtonAction
{
NSApplicationActivationPolicy policy = NSApplicationActivationPolicyAccessory;
//-- hide app icon and window
NSApplication.sharedApplication.activationPolicy = policy;
}
}
To bring the view back, set the policy to NSApplicationActivationPolicyRegular
BTW, I'm assuming we also have a statusbar icon that will be working as an anchor to the app.
来源:https://stackoverflow.com/questions/60921374/maccatalyst-app-how-to-close-a-window-without-terminating-the-app