Stop, Invalidate and set to Nil a NSTimer When User Exits - iphone

I have an application that has a NSTimer running as long as the button is being pressed. However, if the user exits out of the app (Multi-tasks) the timer continues to go. How can I invalidate it so that the time stops and is set to nil when the app is being exited. Can I disable multi-tasking? Thanks. P.S If it involves using the AppDelegate how do I get that NSTimer to be referenced in the AppDelegate, because it is created in the ViewController.h
I have tired using:
#import "Some AppNameViewController.h"
Anyway, thanks in advance!

You can disable multi tasking by setting UIApplicationExitsOnSuspend to YES in your plist.
The more elegant solution is to listen for the UIApplicationWillResignActiveNotification or the UIApplicationDidEnterBackgroundNotification in the class with the timer. When either of those notification are posted invalidate and nil your timer.

You could use NSNotificationCenter and simply add your timer as an observer of a message that you send when the app exits. Example:
In the viewController:
-(void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self.myTimer selector:#selector(invalidate) name:#"killTheTimer" object:nil];
}
In the delegate:
- (void)applicationWillResignActive:(UIApplication *)application{
[[NSNotificationCenter defaultCenter] postNotificationName:#"killTheTimer" object:nil];
}

Related

In terms of iphone application, the way to reload displayed page automatically when a background app comes back to be active

I have developed an iphone application with Phonegap/Cordova v1.9.0.
I want to realize the following matter.
-When a background app comes back to be active(When the app icon is tapped), a displayed page is reloaded automatically-
Probably I should make some programs in a function, (void)applicationDidBecomeActive, in Appdelegate.m or MainViewController.m, but I have no idea what to do.
Please tell me how to solve this case.
You can use the NSNotification observer pattern. In your MainViewController.m file, and viewDidLoad, you can add an observer (registering for notifications):
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
Then you must implement appDidBecomeActive: (you can give the selector any name, but you must implement a method of that name). In this example:
- (void)appDidBecomeActive:(NSNotification *)notification {
NSLog(#"App became active");
}
When the app is resumed, and should this view controller be active, it will simply log that to the console. You can put any code you wish inside that method (in your case, refreshing a page).
Don't forget to remove the observer when the view controller is deallocated in the dealloc method. This will remove all observers for you.
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSLog(#"Removed all notification observers");
}
Are you really using version 1.9?
Try this:
document.addEventListener("resume", onResume, false);
function onResume() {
// Handle the resume event, reload the page or content
}

Execute code after method has finished

I need to execute some code after I know the keyboard is hidden.
Ive been looking in to blocks but I'm just not understanding how they work enough to do this...
All I want to do is run [self hidekeyboard] then when that is complete (and the keyboard fully hidden) then I want to call a delegate.
What is the best way to handle this and how?
You want to use the UIKeyboardDidHide notification and run your code in there. Here is the link in the docs...
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(onKeyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
And the onKeyboardDidHide:
-(void)onKeyboardDidHide:(NSNotification *)notification
{
// execute what you want.
}
Register a listener for the UIKeyboardDidHideNotification using the NSNotificationCenter class.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardHidden:)
name:UIKeyboardDidHideNorification
object:nil];
- (void)keyboardHidden:(NSNotification *)notif
{
// do stuff
}
(Don't forget to remove the observer in - dealloc so that no messages will erroneously be sent to deallocated objects.)
You probably want to register to receive notifications of UIKeyboardDidHideNotification.
http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

send application delegate message to current view without notification center

I want to display application delegate message such as "Application became active" (This is called when -applicationDidBecomeActive:application is called)
on Window.
One way is to use notification center like below:
AppDelegate.m
NSNotification *n = [NSNotification notificationWithName:#"AppBecameActive" object:self];
[[NSNotificationCenter defaultCenter] postNotification:n];
ViewController.m
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(showMessageAppBecameActive) name:#"AppBecameActive" object:nil];
This way is only way to show application delegate message ? Or, is there any other way such as property to look current view controller instance ?
Thank you for your kindness.
If you have access to the ViewController from your appDelegate. (I mean like #property or instance is reside in it) you can straight away send a message. If you do not have such access to it. write key value observer for a single ton and let your viewController receive the change.
Register below code in your viewController which wants notification.
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(yourMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];
iOS framework posts a notification when your app become active, if you will register via above way, you can handle the notification in registered method (In this case yourMethod).

Performing an action on one view, and the action doing something on another view? Is this possible?

I have 2 views:
OneViewController
TwoViewController
TwoViewController has an IBAction which plays a sound. Once the user has pressed the button on TWoViewController I want a UILabel which will appear on OneViewController saying that the sound has been played.
Thanks for the help
All you have to do is reference one viewController in the other one, that way you can call it's methods. Or you can simply create a delegate.
One possible solution is to use notifications.
In the action that plays a sound, post a notification to the default notification center that indicates the sound has played.
[[NSNotificationCenter defaultCenter] postNotificationName:"playSoundNotification"
object:self
userInfo:nil];
When OneViewController is created, have it register for the notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(showPlayedLabel:)
name:"playSoundNotification"
object:nil];
When it receives the notification -- in showPlayedLabel: -- display the UILabel. Note that showPlayedLabel must follow the appropriate signature format.
- (void) showPlayedLabel:(NSNotification*) aNotification;

run animation every time app is opened

I have an animation in viewDidLoad that runs the first time the app is launched. if you exit the app, then launch it again the animation doesn't play.
how would I go about making the animation play each and every time the app is opened,
thanks for any help
In iOS 4, pressing the home button doesn't terminate the app, it suspends it. When the app is made active again, a UIApplicationDidBecomeActiveNotification is posted. Register for that notification and initiate the animation in your handler.
Edit: Added code below.
Here's one way to do it: Have your view controller become an observer of UIApplicationDidBecomeActiveNotification in its viewWillAppear: method.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(performAnimation:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
Unregister for the notification in your view controller's viewDidDisappear: method.
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
Finally, put your animation code in the selector specified when registering to receive the notification.
- (void)performAnimation:(NSNotification *)aNotification {
// Animation code.
}
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
Put the animation in a method like
applicationDidBecomeActive:
of UIApplicationDelegate
Very likely your app isn't quitting and reloading. By default, on iOS 4 apps continue to run when the user 'exits' the app, and continue where they left off when 'restarted'.
Take a look at applicationDidBecomeActive in your app delegate. You could kick off your animation from there when the app is deactivated.
How about set a flag in your application delegate to control this behavior:
Set it to YES when the app enters the foreground or became active (applicationWillEnterForeground:, applicationDidBecomeActive:)
Check if this flag is NO in -viewWillAppear in your view controller:
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if(!delegate.animationPlayed) {
//perform animation here...
delegate.animationPlayed = YES;
}