NSApplicationWillTerminateNotification not Working - objective-c

My problem is that the NSApplicationWillTerminateNotification is not called when I quit my application. What I have tried: (appDelegate.m)
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil];
}
- (void)applicationWillTerminate:(NSNotification *)aNotification{
NSLog(#"quit");
}
OR
- (void)applicationWillTerminate:(NSApplication *)application{
NSLog(#"Quit");
}
My application has no window, as it is a background application -> I deleted the window and the menu in interface Builder.

Apps in the background are terminated without any notification.

Related

Pause game on orientation change, objective-c / Sprite Kit

I want my game to pause when device orientation is initiated. I have this method in my viewcontroller, which works fine:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"I am starting to rotate, should pause game..");
}
But how to listen for device rotation from within my SKScene, where the actual game is playing. Hope I have made myself clear enough. Thanks for any help!
You can use NSNotification. Keeping with your code, add the following line in your ViewController init:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"RotateNotification"
object:self];
Then in your SKScene init add this line:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"RotateNotification"
object:nil];
Also add the called method to SKScene:
-(void) receiveNotification:(NSNotification *)notification
{
NSLog (#"Received notification");
// do what you have to do here...
}

How to close a modal view when the application enter in background on ios

I have a modal view created in a method (there is no reference in the mainview) and I want to do a dismissModalViewControllerAnimated automatically when my app enter in background. How can I do that ?
In the mainview's viewDidLoad, add observer to be notified when app goes to background.
- (void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(goToBackground)
name:UIApplicationWillResignActiveNotification object:nil];
}
Define the function goToBackground(). It will be called when the app goes to background
- (void) goToBackground
{
[self dismissModalViewControllerAnimated: NO]; // no need to animate
}
Don't forget to remove the observer
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You can use a notification. Post a notification from the ApplicationDelegate's method applicationDidEnterBackground:. YOu can call the dismiss method from the modal controller, so add it as observer to the notification center.

iPad : How to know Return key of iPad keyboard is pressed ? Please check image

I want to know that which method will be called when the following key is pressed.
I want to start action on above key press.
How do I know this is pressed ?
Observe the UIKeyboardDidHideNotification notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
And...
- (void)keyboardDidHide:(NSNotification *)aNotification {
}
You can also change it to UIKeyboardWillHideNotification if you need to be notified BEFORE the keyboard starts to disappear.
That's not a return key. Return key is the one above it. That's simply a button that dismisses the keyboard and you can't recognize it via standard text input methods. You need to register for UIKeyboardWillHideNotification notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
and implement that method:
- (void)keyboardWillHide:(NSNotification *)notification
{
// do whatever you want to do when keyboard dismiss button is tapped
}
Not sure if it's exactly what you are looking for, but you can try using notifications. Don't have Mac nearby atm, so just copy-pasting the code from github. I have that code in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
and then 2 methods:
- (void)keyboardWillShow:(NSNotification *)notification {
}
- (void)keyboardWillHide:(NSNotification *)notification {
}
Hope it helps
Use keyboard hide UIKeyboardWillHideNotification notification.
Example.

Removing a NSNotificationCenter observer in iOS 5 ARC

I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController. Similar posts on SO have said this should be done in the -dealloc method. Even though this method is not required in ARC projects I have added it with the following code:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
As a test, I open the UIViewController (within a UINavigationController), do some things which trigger the notifications, and then pop it off the stack by tapping the Back button. I then reopen the UIViewController, and do some more things to trigger the notifications, but notice that each callback is being called twice - an indication that the previous notifications have not been deregistered. Repeating this procedure just causes each callback to be called more than more times, so they appear to never be deregistering.
Any help would be appreciated!
It's pretty clear your dealloc method isn't being called (nor is the removeObserver call).
Why not remove your UIViewController's observer in the viewDidUnload: or viewWillDisappear: methods?
If your dealloc isn't being called, it's likely because someone is still holding a reference to the view controller. Perhaps you need to mark something as __weak? You can use the allocations instrument to help track down what's holding on to your view controller.
"I also need the notification callbacks to still be fired if the view is off-screen" -> you may need to register UIApplicationWillEnterForegroundNotification. If so, let try this:
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"viewWillAppear");
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"viewWillDisappear");
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"applicationWillEnterForeground");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
// do your stuff here
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"applicationDidEnterBackground");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
The idea is adding or removing UIApplicationDidEnterBackgroundNotification whenever coming in and out of your screen. We just register UIApplicationWillEnterForegroundNotification when the app enter background and remove once it's back. Be noticed that we just remove UIApplicationDidEnterBackgroundNotification when viewWillDisappear.
My dealloc() is not called by somehow, so I found this way, hope it useful for you too.
Enjoy :)

Dismiss modal view controller on application exit

I have a view controller (view A) presenting a modal view (B) when the user pushed a button and the view B has itself a button to present view C. My problem is that if the user exits the application when the view B or C is shown, the same view will appear next time the application is launched. Is there a way to dismiss the views B and C on exit or to show view A when the application starts?
Thanks for your help
I assume by close you mean when the application enters the background.
In your app delegate you can via the applicationDidEnterBackground: method dismiss your controller.
Best way would probably be to add an observer in your view controller class:
- (void) viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appClosing) name:#"appClosing" object:nil];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"appClosing" object:nil];
[super dealloc];
}
- (void) appClosing
{
[self dismissModalViewControllerAnimated:YES];
}
And post the notification in your app delegate:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"appClosing" object:nil];
}