Terminating an application when closing the window - objective-c

I'm a little bit new to Objective-c with xCode, and I would like to know something. Is there a way to terminate an application when the red circle in the left of the window is clicked? Like on the calculator.

Yes you can do with Mac OSX applications.
You need to implement this method in your AppDelegate class
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
return YES;
}

If you want terminate the app when closing the window. Please implement the following appdelegate method.
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
If you want do not terminate your app, just set the return "NO".
I hope that your problem will be resolved with this solution.

Related

Simperium, getting delegate notifications of AuthManager

Ok, i am totally stuck and was wondering is anyone could point out what must be the obvious mistake i am making.
I am using Simperium (dev branch) in a project, and want to get a notification in my main AppDelegate if the user dismisses the authentication window.
Now in the SPAutheticationManager.m file is the following code:
- (void)cancel {
DDLogVerbose(#"Simperium authentication cancelled");
if ([delegate respondsToSelector:#selector(authenticationDidCancel)])
[delegate authenticationDidCancel];
}
I have set a breakpoint and this is definitely being called when the window is dismissed.
Now, i have added SPAuthenticationDelegate to my implementation in my AppDelegate, and then added the following code to AppDelegate.m
-(void)authenticationDidCancel {
NSLog(#"Authetication Cancelled");
}
But, this isn't getting called, and i can't work out why???
Anyone have any idea what i'm missing here?
Thanks
Gareth
In case anyone else hits this, there is no way to do this without implementing a custom delegate method in simperium.h and making your AppDelegate.h a delegate of it.
In simperium.h
- (void)didCancelAuth;
Then in simperium.m authenticationDidCancel method add:
if ([delegate respondsToSelector:#selector(didCancelAuth)]) {
[delegate didCancelAuth];
}
Then set your appDelegate as simperium's delegate and add:
- (void)didCancelAuth
{
//auth has been cancelled
}
you also need to make sure your appdelegate is a delegate by doing something like
self.simperium.delegate = self;
Cheers
Gareth
Just wanted to let you know that we've just added a brand new 'login cancelled' delegate method (Commit here: https://github.com/Simperium/simperium-ios/commit/5cae8a157786a48ffe1cc649f898341eb9cf51bf in develop branch).
Thanks for helping us improve Simperium!

iOS 6 Game Center Crash on Authentication in landscape mode cocos2d

I have developed a game in cocos2d and all game screens are in Landscape mode. I am trying to implement game Center but getting crash on authentication. I did not find answer of similar type of issues. please suggest right approach...
Crash issue:-'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'
I tried below solution but it also disturb game orientations, game starts work in portrait mode also, that i don't want:-
(NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow: (UIWindow*)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
Make sure you selected landscape in Xcode summary page.
Also add these code in your viewcontroller
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Update this function in AppDelegate:
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskLandscape;
}
the solution for that is short, i spent a lot of time before finding it:
in the AppDelegate in the method didFinishLaunchingWithOptions put this line:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
obviously before call the login game center method, i put that before create the UIWindows

Menu Bar App Never Becomes Reactivated

I'm building a Mac app that only sits in the menu bar with no dock item and no key window and no main menu (it's LSUIElement in the info.plist is set to YES). When I first launch the app, applicationDidBecomeActive: is called, as I expect. However, once another app gains focus, applicationDidBecomeActive: is never called again.
This prevents a text field I have within my app from becoming the first responder. When I first open the app, the text field is editable:
But after another app comes to the foreground, the text field is not editable:
What I've tried:
When the menu is opened, menuWillOpen: is called on the NSMenu's delegate. I've tried placing the following with no success:
[NSApp unhide];
[NSApp arrangeInFront:self];
[NSApp activateIgnoringOtherApps:YES];
[NSApp requestUserAttention:NSCriticalRequest];
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
[[NSRunningApplication currentApplication] unhide];
I think the issue is probably related to not having any windows to bring to the front. I feel like I'm grasping at straws here. Any help would be greatly appreciated.
I think the issue is with that how the runloop operates when a NSMenu is open, so you should try activating the app before you display the menu. If you're having the NSStatusItem display it, I'd suggest doing it yourself like this:
- (void)toggleMenu:(id)sender
{
// App might already be active
if ([NSApp isActive]) {
[self.statusItem popUpStatusItemMenu:self.menu];
} else {
[NSApp activateIgnoringOtherApps:YES];
}
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
[self.statusItem popUpStatusItemMenu:self.menu];
}
That should work, but I think though in general you'll have better luck with an actual window instead of a menu.
You probably need to allow your input to -becomeFirstResponder, maybe by overriding -canBecomeFirstResponder or by calling the become method yourself.
You'd likely have to implement/call these methods for whatever view is housing your text input, or maybe tell your input view to become the first responder.
Either way, it smells like a responder chain issue.
Try calling -makeFirstResponder: on your window. NSWindow is usually the start of the NSResponder chain.
- (void)menuWillOpen:(NSMenu *)menu {
[[NSApp mainWindow] makeFirstResponder:yourTextInputField];
}
I'm assuming your text field already accepts first responder since you said your app launches initially with it as the first responder. If not, make sure your text field overrides -acceptsFirstResponder: to return YES
- (BOOL)acceptsFirstResponder {
return YES;
}
Edit: Ah, see that you don't have a key window. It looks like NSMenu actually has a window associated with it though, and it's safe to call -makeFirstResponder:. Some discussion here suggests overriding -viewDidMoveToWindow: on your view containing your text field in the NSMenu like so:
- (void)viewDidMoveToWindow {
[super viewDidMoveToWindow];
[[self window] makeFirstResponder:yourTextInputField];
}

Terminate application on exit

how could i make my Xcode application terminate completely when the user presses the X button.
Could you also provide some detail cause i'm sort of a beginner.
EDIT: Sorry for my vagueness, This is a desktop application.
Use - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender method of AppDelegate. Sample:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return YES;
}
Send the following message: [NSApp terminate:nil].

How do I run modals windows from code with cocoa?

I`m trying to run a window as modal directly from the code.
My program starts and a main NSWindows is showed.A thread still running to see if the user has a valid distribution. if he doesn't I need to run a modal.I mean, I have no buttons clicked in the interface.I've designed a NSWindow on the interface builder for a password set, and I want to call it only when my validation is not successful.
I have tested and realized that these methods which are responsible for modal windows running only work in a IBAction environment.
//This doesn't work
-(void) showPasswordWindow
{
[NSApp runModalForWindow:[self window]];
}
//this works But its not useful for me =(
- (IBAction) passwordWindowButton:(id)sender
{
[NSApp runModalForWindow:[self window]];
}
Please, help this newbie =)
One thing to check: are you calling the method on the main thread?
to check, add this to showPasswordWindow
NSLog(#"Main thread? %d", [NSThread isMainThread]);