Search code examples
iosuiapplicationdelegatehome-button

Pausing iOS app game when User clicks Home Button


While the user is playing the game if they tap the Home Button on iPhone/iPad the game pauses itself but doesn't stay paused if the user clicks back on the app to open it, instead game leads them to game over screen. How do I code it that once the user clicks Home Button the game pauses and if they click back on app, within 2 seconds the game unpauses and you continue. Is there a particular code that goes inside the following methods that pauses the entire game and then unpauses it after a someone clicks on it and a few seconds go by?

  UIApplicationDelegatedelegate 
  -(void)applicationWillResignActive:(UIApplication *)application{}
  -(void)applicationWillEnterForeground:(UIApplication *)application{}

I got the app to pause in the background and stay paused but how do I make it that when the user EnterForeground again, there's about a 2 second delay before the game un-pauses?


Solution

  • iOS can't magically pause or un-pause your game for you - it's something you have to handle on your own in the code.

    Once you have the logic to un-pause the game you can get the 2 seconds delay you mentioned in your question by calling performSelector:withObject:afterDelay: (see the docs). For example:

    [self performSelector:@selector(continueGame) withObject:nil afterDelay:2.0]
    

    where self has a method called continueGame to .. continue the game. :)