I'm new too Xcode (5.1.1) and Mac's (OS X) and I'm trying to get my app to close when the red close button is pressed. I am using the below code to try and do it in AppDelegate.m
- (void)windowWillClose:(NSNotification *)aNotification {
[NSApp terminate:self];
}
But all this seems to do is close the window, the app itself is still running and the icon remains in the dock. Though clicking the icon does nothing and neither does launching the app again (it seems to think it is still running). I have to force shut down in order to launch the app again.
Any help would be great thanks.
If shutting down the application when the last window closes is acceptable you can use the following method in your app delegate.
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
if you don't want to quit the application, you should do this :
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return NO;
}
Related
When I launch a cocoa app from terminal, the window generated by the cocoa app stays behind the terminal. Is there a way to keep the window on top?
I tried in the cocoa app :
-(void) awakeFromNib {
[self.window makeKeyAndOrderFront: self];
[self.window setLevel: NSMainMenuWindowLevel];
}
also tried with setLevel: NSFloatingWindowLevel. Neither works.
or Should I put the [self.window setLevel: NSFloatingWindowLevel] somewhere else?
Anyone can give some suggestions? Thanks.
LJ
You need to make your window the key window and then it would come on top of other windows.
[NSApp activateIgnoringOtherApps:YES];
[self.window makeKeyAndOrderFront:self]
And the correct method is awakeFromNib not awakerFromNib. Ignore if you have declared awaker method.
I've created a Mac OS X application, and during the development it was fine because I opened it with Xcode.
But now, I try to execute the executable and it's working only the first time, because after close the window, the app persists in memory. And I have to kill it with the activity monitor.
So what should I do to kill the app when I touch the red top left button on the window?
- (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application
{
return YES;
}
Override the windowWillClose method in your delegate to make it terminate the app.
- (void)windowWillClose:(NSNotification *)aNotification {
[NSApp terminate:self];
}
I am creating an app in which I want to give the user the ability to show or hide the dock icon at run time. I have a preferences window with a checkbox, setting a user default value, which fires the following code using KVO:
if (!hideDockIcon) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
} else {
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
}
This works, but when hiding, the preferences window is closed directly (which makes sense as it is now a background app). However, I noticed that MS's SkyDrive client manages to hide the icon while keeping the Preferences window open. I have not been able to find out how one would do that, anybody has an idea?
I also tried using [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular] and NSApplicationActivationPolicyAccessory/NSApplicationActivationPolicyProhibited but that doesn't work for me; Accessory doesn't hide the dock icon, Prohibited closes the window as well and seems to make [NSApp activateIgnoringOtherApps:YES] being ignored.
I stumbled upon this thread where the following is suggested to prevent a window from being hidden:
[window setCanHide:NO];
This just covers hiding. If your window gets closed, you might try to use the window delegate?
There's a call that let's you prevent the window from being closed
- (BOOL)windowShouldClose:(id)sender
I solved this problem by not activating the app in the same run loop turn:
dispatch_async(dispatch_get_main_queue(), ^{
[NSApp activateIgnoringOtherApps:YES];
});
Swift:
dispatch_async(dispatch_get_main_queue()) {
NSApp.activateIgnoringOtherApps(true)
}
I'm calling dispatch_async to schedule the block for execution in one of the next run loop turns a few nanoseconds later. This gives the process the chance to finish hiding itself.
I added a applicationShouldOpenUntitledFile method to my application delegate, returning NO as Apple's documentation specifies. However, I'm still getting a new document on startup. What's wrong?
#implementation AppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog( #"This is being called" );
}
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
NSLog( #"This never is" );
return NO;
}
#end
You're running Lion. When you ran before adding the applicationShouldOpenUntitledFile handler, a new document was created. Now, with 10.7's "Restore windows when quitting and re-opening apps", your application is restoring that untitled window, and not creating a new one as you suppose.
Close that window and re-run your application, and applicationShouldOpenUntitledFile will be called and will suppress the creation of a new untitled file.
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Schedule "Checking whether document exists." into next UI Loop.
// Because document is not restored yet.
// So we don't know what do we have to create new one.
// Opened document can be identified here. (double click document file)
NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:#selector(openNewDocumentIfNeeded) object:nil];
[[NSOperationQueue mainQueue] addOperation: op];
}
-(void)openNewDocumentIfNeeded
{
NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];
// Open an untitled document what if there is no document. (restored, opened).
if(documentCount == 0){
[[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
}
}
I'm using Xcode 8.3.2 and compiling for Os X 10.11 using a storyboard for a document based app.
I noted that, if you set the window controller as initial controller, a window is created without any document and without calling applicationShouldOpenUntitledFile.
I solved removing the "is initial controller" checkbox in the storyboard.
If you're not running Lion / 10.7 or later, this can still happen if you have some other window open (even a non-Document window) when applicationShouldOpenUntitledFileshould be called.
I have a Document-based app where the AppDelegate class opens a global logging window, both for debugging purposes and for user status messages. If I have the program display that window on startup while running on OS X 10.6, applicationShouldOpenUntitledFile never gets called, even with no document windows displayed. If I turn that window off, the call is made.
Since OSX Lion, the app's state restoration may interfere with your custom preferences for this exercise.
Citing an update to Aaron Hillegass and Adam Preble's book Cocoa Programming for MacOSX:
Note that Mac OS X Lion's state-restoration features may make it tricky to observe the new document preference. You can disable state restoration by editing the Run scheme in Xcode. Open the product menu and select Edit Scheme. Select the Run RaiseMan.app scheme, change to the Options pane, and check Disable state restoration.
I've been trying to submit an application for the Apple App store and the reviewer claims that my initial notice window (which is modal) does not go away when they hit the accept button. Now I've tested it on several machines (even clean 10.7.1 installations) and have not seen this behavior (it works great for me).
My code to display this window:
- (IBAction) doAlert:(id)sender {
if(self.alertVC == nil) {
self.alertVC = [[[AlertVC alloc] initWithWindowNibName:#"AlertVC"] autorelease];
}
[NSApp runModalForWindow:self.alertVC.window];
}
and the code with which I close it looks like this:
-(IBAction)closeWindow:(id)sender {
[self close];
[NSApp stopModal];
}
Anyone have any idea why this works 100% in all my tests but for the Apple testers the window does not go away? (It stays open in the foreground even though the application continues and they can interact with the application as normal again.
The only thing I see, that may cause the error is, that you are not closing the window, but the controller (or who else provides the method closeWindow)
If closeWindow is a method of the same class than doAlert the code should look like:
-(IBAction)closeWindow:(id)sender {
[self.alertVC.window performClose:self];
[NSApp stopModal];
}
I know the answer is late and you probably have found a solution, but I provided the answer for all those stumbling over the snippet and wondering, why it happens to them as well.