run animation every time app is opened - iphone

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;
}

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
}

iPhone: Can an app detect when the phone receives a call and come back to the app?

I am checking it with
- (void)applicationWillEnterForeground:(UIApplication *)application
when it comes back to the app.
But this one doesn't detect when the user declines the call. Is there anyway to detect even when the user declines the call?
Maybe you could use the following notifications, in your case, the second one:
Add CoreTelephony.framework to your project and:
#import <CoreTelephony/CTCall.h>
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(callReceived:) name:CTCallStateIncoming object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(callEnded:) name:CTCallStateDisconnected object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(callConnected:) name:CTCallStateConnected object:nil];
I think what you want is to detect when the app is coming back into an active state.
There are two delegate methods for this:
applicationWillEnterForeground:
Tells the delegate that the application is about to enter the foreground.
- (void)applicationWillEnterForeground:(UIApplication *)application
and
applicationDidBecomeActive:
Tells the delegate that the application has become active.
- (void)applicationDidBecomeActive:(UIApplication *)application
more info in the documentation of UIApplicationDelegate
There isn't a way that I know of to tell when the call is declined. But this method is called when the user gets a call:
- (void)applicationWillResignActive:(UIApplication *)application {
}

how to hide UipickerViewController in applicationWillResignActive?

I my application i want to hide the UIPickerview controller when application enter's in a paused state?
for eg: in my viewcontroller i am using uipickerview,and is placed in a popovercontroller,but i have not dismiss the popovercontroller and the application goes in a paused state.
when user open the application the uipikcer view is displayed which i want to dismiss.
please help me out with this.
Use NSNotificationCenter for hide the UIPickerView when application go in background like bellow...
[[NSNotificationCenter defaultCenter] addObserver:alertView selector:#selector(HidePickerView) name:#"UIApplicationWillResignActiveNotification" object:nil];
and hide the UIPickerView in bellow method..
- (void) HidePickerView {
[yourPickerView setHidden:YES];
}
You need to subscribe for the UIApplicationWillResignActiveNotification notification and when it's fired and your selector is called, you should [self.myPickerView dismissViewControllerAnimated:NO completion:nil];.
Note: -viewWillDisappear: method would not be called when the app is resigning active.

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

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

Returning from dismiss modal view controller - any way to detect?

Wonder if anyone can help me. I have a setup whereby a main menu presents the main game as a modal view controller. At this point it also stops playing the main menu music.
The problem is that when the game view controller dismisses itself (e.g when user quits game) and returns to the main menu I cannot get the main menu music to start playing again.
Is there some way I can get the music in the main menu to start playing again ? E.g a delegate method that is called when the main game dismisses ?
Thanks,
Martin
You can do it with the NotificationManager
// set up notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMusic:)
name:#"musicNotification"
object:nil];
// send notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"musicNotification"
object:self];
// clean up notification
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
- (void) playMusic:(NSNotification *) notification
{
// play the music here
if ([[notification name] isEqualToString:#"musicNotification"])
NSLog (#"Received musicNotification!");
}