I don't know if by Apple's rules it's necessary to implement that when a user is in the middle of a game, if they click on home button the game pauses and when they click back in game, the game unpauses.
Do I have to click something on storyboard for a home button to appear? I don't know.
Since storyboards has no home button on it so how do I code that when user clicks home button to exit the app, the game pauses. When they return again, the game unpauses.
There are two way to implement it:
1: use
NSNotificationCenter
:2: Use
UIApplicationDelegate
delegate MethodIf you are making a simple game, you don't have to implement anything for it to pause. The memory management mechanism will take care of it.
However if you are making a game that needs some features say internet connection, you will have to handle what to do with your tasks. For instances, please checkout the apple's UIApplicationDelegate reference page. Check out "Managing State Transitions" section to have a better understanding.
If the home button is clicked, your App Delegate's method
applicationDidEnterBackground:
is called. Thus, you can respond as you see fit.However, you might not need to do anything special here, because when your app goes into the background, it is suspended — it stops running. Thus, in a sense, it pauses automatically. Timers are paused, for example. (Note that this feature, the automatic pausing of timers, is broken in the iOS 8 simulator; but it works correctly on a device.)
In the iOS simulator, you can "press the home button" with the CMD+SHIFT+H shortcut.
You can override the following methods in your UIApplicationDelegate implementation:
Or you can use NSNotificationCenter and the equivilent notifications:
When iOS puts your app into the background, it is essentially paused, in that it is no longer running (with some exceptions for things like music players, GPS consumers, etc). But your render loop isn't doing anything. However, your app may get killed while in this state, so you probably want to do some bookkeeping to keep your apps state consistent. It wouldn't hurt to pause your app, save the users progress, etc, and then resume/restart and load that info when you come back.