How to call scene method before application goes to background? - ios

I need to pause the game before my game goes to background, so when it comes to foreground again I see the pause dialog in my game. To show pause dialog I have a scene method. How to call scene method right before application goes to background? I see that the following method is the right place:
-(void) applicationDidEnterBackground:(UIApplication*)application
{
if( [navController_ visibleViewController] == director_ )
[director_ stopAnimation];
}
Should I just get the scene from director runningScene, cast it to my scene class and then send pause message to it or that way is ugly?

In this scenario, I think Notifications are the cleanest and safest option. You can just post a notification from applicationDidEnterBackground and receive wherever you want to perform certain actions like:
In App Delegate:
- (void)applicationDidEnterBackground:(UIApplication*)application {
[[NSNotificationCenter defaultCenter] postNotificationName:#"EnteringBackground" object:nil];
}
In GameScene:
- (void)onEnter {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showPausePopup) name:#"EnteringBackground" object:nil];
}
- (void)onExit {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)showPausePopup:(NSNotification*)notification {
// Code to show popup
}

Related

Notification fired twice even tho adding observer only once

I have two class which uses NSNotification to communicate with each other.
Currently, i have an issue with notification being fired twice, i've double/triple/even more checked that observer is not added more then 1 time, notification not being posted twice, did global search on my project for same notification.
My code is like below
Added Notification Observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:notification_deleteMediaFromGallery object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationReceiver:) name:notification_deleteMediaFromGallery object:nil];
Notification Receiver
- (void)notificationReceiver:(NSNotification*)notification {
if ([notification.name isEqualToString:notification_deleteMediaFromGallery]) {
if ([[notification.userInfo objectForKey:#"kind"] integerValue]==GalleryKindPhoto) {
//My statements
}
else if ([[notification.userInfo objectForKey:#"kind"] integerValue]==GalleryKindVideo) {
//My statements
}
}
}
Post Notification
dispatch_async(dispatch_get_main_queue(), ^{
[_browser reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName:notification_deleteMediaFromGallery object:nil userInfo:#{#"index":#(_browser.currentIndex), #"kind":#(self.kind), #"function":[NSString stringWithFormat:#"%s",__PRETTY_FUNCTION__]}];
});
I have also tried this solution by EmptyStack but not get it to work.
I'll be very thankful to you if you could help me solve this issue.
Thanks.
Edit
NOTE
I've added observer in my viewdidload, and cant add/remove observer from viewwillappera/viewwillappear or viewdidappear/viewdiddisappear because the next viewcontroller which will be pushed on current viewcontroller will post notifications
I think you need to write dealloc method in your view controller. And remove All Notification observer in dealloc method,
- (void)dealloc
{
// Deregister observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:notification_deleteMediaFromGallery object:nil];
}
Hi please make sure your method is not calling two time from where you are firing notification.
& please add your notification observer in viewWillDisappear method.

pausing spritekit game using appdelegate in ios8

I have a pause menu in my game that I want to toggle when the i hit my home button. it works when I'm within my game, however when I hit my home button the menu comes up, but the game will not pause.
when i restart the app, the menu is still there, but the game un-pauses itself. It seems like spritekit automatically pauses and restarts the game on its own when you leave and enter the app. When I'm within the app I have full control over it. But when I'm exiting/entering the app it behaves how it likes.
Any suggestions?
func applicationWillResignActive(application: UIApplication!) {
// get the root viewcontroller
// toggle my pausemenu method. pause menu sets skview.paused = true
}
Send an NSNotification to GameSceneViewController to pause the SKScene:
AppDelegate:
- (void)applicationWillResignActive:(UIApplication *)application
{
// Pause sprite kit scene
[[NSNotificationCenter defaultCenter] postNotificationName:#"PauseGameScene" object:self];
}
GameSceneViewController:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(pauseGameScene)
name:#"PauseGameScene"
object:nil];
}
- (void)pauseGameScene {
if (self.skView.scene) {
self.skView.paused = self.skView.scene.paused = YES;
}
}

View Controller method for lock button pressed?

Is there an available view controller method that is called when the user presses the lock button? I'm looking for something like viewDidDisappear: or viewWillDisappear:, but specific to the case of the lock button being pressed.
A notification called UIApplicationDidEnterBackgroundNotification is posted when the user locks their phone. Here's how to listen for it:
In viewDidLoad: of your ViewController:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(screenLocked) name:UIApplicationDidEnterBackgroundNotification object:nil];
Then, define a method (mine was called screenLocked above) and write code you want to be executed when the screen is locked.
-(void)screenLocked{
//do stuff
}
Also, to do some necessary cleanup, add this method to your ViewController too.
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
Try this :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
NSLog(#"Sent to background by locking screen");
} else if (state == UIApplicationStateBackground) {
NSLog(#"Sent to background by home button/switching to other app");
}
}

I need to send a message to a method every time my app comes back from background

I'm developing an iOS app with latest SDK.
It's a fullscreen app.
I have a method on viewWillAppear method that has to be called every time the apps comes from background.
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setUpVideo];
}
On setUpVideo I set up AVCaptureVideoPreviewLayer because I lose the video when the apps come back from background.
As I have read, viewWillAppear isn't called when the apps come back from background and now, I don't know where to put that code.
On this question, occulus suggest to use [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; but it doesn't work for me.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(setUpVideo:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}
Any advice?
Observe UIApplicationWillEnterForegroundNotification instead.
- (void)viewDidAppear {
[super viewDidAppear];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(enterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
// ...
}
- (void)enterForeground:(NSNotification *)notification {
// do stuff
}
Don't call viewWillAppear: directly from the enterForeground: method. Instead move all required code to a separate method and call that from both viewWillAppear: and enterForeground:.
applicationWillEnterForeground will trigger when app comes from background
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
Additionally, you can use UIApplicationDidBecomeActiveNotification for firing some method
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(handleMethod:)
name: UIApplicationDidBecomeActiveNotification
object: [UIApplication sharedApplication]];
Try posting this notification from
- (void)applicationDidBecomeActive:(UIApplication *)application of AppDelegate(or observe corresponding notification which is better)

Detect when app finishes loading after opening from background

When my app is first opened there is a long loading time so I can display a loading screen. When the user exits the app by clicking the home button then re-opens it (the viewDidLoad/viewDidAppear methods aren't called again) the app has another loading period, I guess while it's "waking up".
What method can I use to detect the user hitting the home button to send the app into the background and what method can I use to detect that the app has been revived from the background?
This should be sufficient enough to provide my loading screen properly but just in-case. Is there also a method for detecting when the loading has finished after a "revival"?
You can register a notification for UIApplicationWillEnterForegroundNotification. There you can do your stuff.
- (void)viewDidLoad
{
[super viewDidLoad];
// Register for the notifcation
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(refreshView) name:UIApplicationWillEnterForegroundNotification object:nil];
}
-(void)refreshView
{
/*
Invoked when application enters foreground. Do your stuff
*/
}
To remove observer
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
Albert you can detect home button click on this two method declared in AppDelegate
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
and these are notification you can use
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(setFlag:)
name: UIApplicationWillResignActiveNotification
object: [UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(setFlag1:)
name: UIApplicationDidEnterBackgroundNotification
object: [UIApplication sharedApplication]];

Resources