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

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:

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

Data is not reloaded when app comes from background

Actually the requirement is to reload the data when app comes from background. but it doesn't reload when I come from background. I write the method for reloading data on viewDidLoad.
So, where should I write the code to solve my problem?
Thanks...
As KartikArora is implies above, your viewDidLoad is not called when the app comes from the background to the foreground. So the data is not reloaded.
You could reload the data whenever the view appears instead of when the view is loaded. But then it would reload the data every time the view appears, which you might not want.
You could also have a reload method in your view controller that is called when the app enters foreground triggered via a posted notification.
-(void) viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod:) name: UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)myMethod:(id)not {
// code for save data
}
try this
Try it in App Delegate File it will work
- (void) applicationWillEnterForeground: (UIApplication *) application
{
write your code here
}
Either you do it in the UIApplicationDelegates
- (void)applicationDidBecomeActive:(UIApplication *)application
method, or you subscribe your object to the UIApplicationDidBecomeActiveNotification.

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.

What method is called when application appears from background on 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

How to react to applicationWillResignActive from anywhere?

What's the code to subscribe to an event like applicationWillResignActive in any place in your iphone application?
[UPDATE]
Let me rephrase my question. I don't want to respond to this in my application delegate, but rather listen to this event from another class. Is that possible or I need to pass the event from the application delegate to the concerning class?
Looks like you are looking for this code.
- (void) applicationWillResign {
NSLog(#"About to lose focus");
}
- (void) myMethod {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];
}
Take a look at the documentation for the method you're talking about:
applicationWillResignActive:
Tells the delegate that the application will become inactive. This method is optional.
- (void)applicationWillResignActive:(UIApplication *)application
[...]
Discussion
[...]
Just before it becomes inactive, the application also posts a UIApplicationWillResignActiveNotification.
Implement the method below in your application delegate:
-(void)applicationWillResignActive:(UIApplication *)application
This allows you to react when the application becomes inactive - when this is the case, it is executing but not dispatching incoming events. This happens, for example, when an overlay window pops up or when the device is locked.
Just before it becomes inactive, the application also posts a UIApplicationWillResignActiveNotification.
Your topic and question are asking slightly different things.
Your application will receive applicationWillResignActive, along with applicationWillTerminate, automatically. No subscription is necessary, just implement the function in your app.
As to how to respond, this is down to the application. While you can choose to do nothing the recommended behavior is that you cease or slow any non-critical functionality. E.g. if you were a game you would stop updating the display and/or pause the game.