Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pausing Game when someone calls/SMS you

I want to implement this function on my apps but i cant seem to figure out how to use this line of codes.

- (void)applicationWillResignActive:(UIApplication *)application {
 //our app is going to loose focus since there is an incoming call
 [self pauseGame];
}

- (void)applicationDidBecomeActive:(UIApplication *)application{
 //the user declined the call and is returning to our app
 [self resumeGame];
}

I've read that this must be placed in appdelegates but i cant seem to figure out how could i call my pause action when the game is currently in the viewcontroller.

Thank you.

like image 462
Drahc Avatar asked Dec 05 '25 10:12

Drahc


2 Answers

Instead of sending the messages to self (which is the app delegate), you would send them to your view controller.

For example, if your app delegate had a property for your main game view controller named "gameViewController" (where the methods to pause and resume were implemented):

- (void)applicationWillResignActive:(UIApplication *)application {
    // our app is going to loose focus since there is an incoming call
    [self.gameViewController pauseGame];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // the user declined the call and is returning to our app
    [self.gameViewController resumeGame];
}
like image 159
gerry3 Avatar answered Dec 07 '25 01:12

gerry3


I know that this was answered a long time ago. But I wanted to add that another (more scalable) solution is to have interested parties (e.g., UIViewControllers) register an interest in UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification.

This approach has the advantage that the Application Delegate doesn't need to have direct knowledge of the objects that might need to respond to entering background/foreground.

like image 32
westsider Avatar answered Dec 06 '25 23:12

westsider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!