What method is called when application appears from background on iPhone? - iphone

I know that when iphone application goes to background, these methods are called:
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillResignActive:(UIApplication *)application
what method(s) are called when application appears from background?
are there any methods in ViewController which are called?
thanks

Along with the applicationDidBecomeActive: and applicationWillEnterForeground: messages sent to the application delegate, the OS will also send corresponding UIApplicationDidBecomeActiveNotification and UIApplicationWillEnterForegroundNotification notifications.
You can have your view controller listen to these notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appWillEnterForegroundNotification:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
Don't forget to remove yourself as an observer before your view controller gets destroyed.

– applicationDidBecomeActive:
– applicationWillEnterForeground:
Oops didnt read your question properly. These two methods are in the UIApplicationDelegate
– viewWillAppear:
– viewDidAppear:
And those are in UIViewController

Related

Pausing game during call

Where can I handle code for pausing game during incoming call. I know about
-(void)applicationWillResignActive:(UIApplication *)application
but during game, how can I call it (I can't call it from SKScene, nor from view controller - only from app delegate, but how can I from there run my piece of code belonging to view controller).
Thank you.
Meanwhile I figured it out, I used NSNotification, here is the line of code, put it inside view controller you want to handle it :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pauseGame) name:UIApplicationWillResignActiveNotification object:nil];

How can a viewController know when an app did just finish launching?

I've made a simple single view program in xcode 4.3.1. I'd like the view to do different things depending on if it is being loaded the first time the application starts vs when it is being resumed.
Can anyone tell me the best way to do this?
The appDelegate has no reference to my viewController so I'm not sure I can pass a variable from my AppDelegate didFinishLaunchingWithOptions method.
How does the AppDelegate communicate with the ViewController when the ViewController does not seem to be instantiated anywhere?
Thanks!
You can use NSNotificationCenter to know when the application enters the foreground, and then you can register individual VCs to care about the event. For example:
- (void)loadView {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
The VC registers during loadView (or any other method). Then when the app enters the foreground, the method
- (void)applicationWillEnterForeground;
is called. Just remember to unregister in dealloc or viewDidUnload.
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
You can use [application:didFinishLaunchingWithOptions:] to determine if you just started. It is only called once when you launch. You can combine that with setting some flags and
[applicationWillEnterForeground:(UIApplication *)application] to determine if you launched or are simply returning to the foreground.

- (void)applicationWillResignActive:(UIApplication *)application Not invoked

im trying to apply a method in this method from a view controller in my game
when a call received to pause the game.
- (void)applicationWillResignActive:(UIApplication *)application
any idea?
The other answers are correct in that -applicationWillResignActive: is called on the application delegate so you just have to have that method written in your delegate to respond to that event. However if you want to write code in your view controller to listen for this event you can register for the UIApplicationWillResignActiveNotification from your view controller. For example:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(pauseGame:)
name:UIApplicationWillResignActiveNotification
object:nil];
See Apple Documentation search for UIApplicationWillResignActiveNotification.
This is a method from UIApplicationDelegate protocol and it must be called in your application Delegate class when the screen locks or an incoming call is received. You should not call this method by yourself
applicationWillResignActive is called by iOS when your app is going to be interrupted, you shouldn't be calling it by yourself.
if you'd like to have some pausing logic for when you app goes to background you should implement it in you delegates -applicationWillResignActive: or -applicationWillEnterForeground:

App Delegate - Unload View Controller

I'm trying to unload a view controller from view when the iPhone goes to sleep. My app has a stopwatch that has to keep counting when the phone goes to sleep or call comes in or the user closes the app without logging out.
I have all this functionality in place, I'm capturing all start times and stop times and upon re-entering the stopwatch view controller, I calculate the difference. It all works beautifully. When I was doing some additional testing I realised I hadn't catered for the iPhone going into sleep mode.
So all I need to do to make sure my stopwatch is correct bring the user back to the app home screen. I know the following method is called when the app goes to sleep:
-(void)applicationWillResignActive:(UIApplication *)application
How do I unload the stopwatch view controller from my app delegate ?
---- UPDATE ----
kpower, thanks for your feedback. I've implemented the following code:
In my App Delegate:
- (void)applicationWillResignActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"AppIsAsleep" object:nil];
}
In my view controller, I have the following:
-(void)viewDidLoad
{
// Add Observer.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewDidUnload:) name:#"AppIsAsleep" object:nil];
}
- (void)viewDidUnload {
//Remove the Observer.
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"AppIsAsleep" object:nil];
}
When the phone goes to sleep, it actually closes the app, am I doing something wrong ?
Regards,
Stephen
You can use the Notifications mechanism. It allows you to unload view controller from different place (not the AppDelegate) this case.
For example, in your view controller's viewDidLoad method you add an observer (don't forget to remove it in viewDidUnload) and in applicationWillResignActive: method of AppDelegate you just simply post notification. That's all.
↓ Update here ↓
When you get a notification - you should manage view controller's removing by yourself. And calling viewDidUnload here is not the solution, cause this method is called after view controller was already unloaded and doesn't cause removing.
How to remove? Depends on how the view controller was added (for example, popViewControllerAnimated for UINavigationController). The main idea here is to make object's retain count equal to 0 (as you know this case an object will be destroyed) - so you should sent release message necessary amount of times.

Determine if UIViewController is closing due to application exit?

Is there a way to test in viewWillDisappear if it's being called because the application is exiting, versus normal ways of it being dismissed? The method applicationWillTerminate in the App Delegate is called after the current view is closed. I want to do different things depending on whether it's being dismissed due to a IBAction or the user clicking the menu button.
Thanks!
You should use observe the UIApplicationWillTerminateNotification in your controller, set a flag, and then check for the flag in your viewWillDisappear implementation.
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:#selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];
I haven't used it for your purposes yet, but the UIApplicationWillResignActiveNotification notification might occur before applicationWillTerminate is called.
Just throw...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillResign:) name:UIApplicationWillResignActiveNotification object:NULL];
... into your UIViewController to test it out.