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];
Related
I have SKScene controlled by ViewController. Inside this SKScene I have an playing board object (derived from NSObject) with some procedures. I need to post a notification from one of it's procedures (playing board procedures). Inside ViewController I have an observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(rowBangEmitAtPosition:) name:#"rowBangN" object:nil];
and inside playing board's procedure I have :
[[NSNotificationCenter defaultCenter] postNotificationName:#"rowBangN" object:self userInfo:#{#"position":point}];
The object is part of SKScene (as variable). But this notification is never triggered. Can someone help me please.
I figured it out - when I put into posting code object:nil instead of object:self, it is working. i don't know why, but it works :).
Thanks to all.
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.
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:
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.
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.