Terminate application on exit - objective-c

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].

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!

Terminating an application when closing the window

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.

Suspending keyboard when user press on home button?

I am developing an application were everything is working fine, except one i.e. when user press on home while keyboard is in active and again opens my application the view frame bounds are changing and moving out of bounds. My expected result is keyboard should get suspended or the view should stay in the same position when it is come back from background to foreground with keyboard in-active state.
I hope people understand my scenario and reply ASAP.
Thanks.
I have found the solution to my question, i hope people can use my solution. Below is the code what I have done,
Add the below line of code in your RootViewController file (i.e. which view is coming at first when you open your APP).
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
And then add a private method as below
- (void) receivedNotification:(NSNotification *) notification
{
if ([username isFirstResponder])
{
[username resignFirstResponder];
}
else if ([password isFirstResponder])
{
[password resignFirstResponder];
}
}
I hope it help some body,Thank u.
Further assistance please see the mentioned link,
there is a method in the app delegate
- (void)applicationDidEnterBackground:(UIApplication *)application
this method is fired when you press the home button.
do the necessary changes(textField resignFirstResponder) in this method and it should work fine i guess.
EDIT here's the code
in the class where you have your textfield create a method
-(void)performWhenHomeBtnprssed
{
[MytextField resignFirstResponder];
}
then in
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[myClassObj performWhenHomeBtnprssed];
}
also i agree with #valexa you should find the root cause of the problem
In software development it is always better to address the root causes than to patch the effect, in your case there are problems with the positioning of your views and you should address that, foreground/background cycling should not affect the views positioning.

NSAlertPanel not working when Application is quitting

Im trying to allow the user to decide whether to quit the application or not and Ive been trying to implement it using this:
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
int answer = NSRunAlertPanel(#"Quit", #"Are you sure?", #"Quit", #"Cancel", nil);
if (answer == NSAlertDefaultReturn) { return NSTerminateNow;
} else { return NSTerminateCancel;
}
}
I have placed this in my AppDelegate.m and linked the delegate to my main window in interface builder. When i debug and run the application in Xcode, and press the close button, the app window closes but the alert panel does not pop up..
Am i doing something wrong? thank you for your help!! I am new here and I hope someone can help me with this.. Thank you so much!
By default closing a window will not quit the application, thus not triggering your alert panel. To make the application quit and show your alert when the user closes the window just add this to the delegate:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)aApplication {
return YES;
}

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]);