Stop Game App if Home Button clicked iOS [closed]

怎甘沉沦 提交于 2019-12-25 18:06:14

问题


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.


回答1:


There are two way to implement it:

1: use NSNotificationCenter:

- (void)setupBackgroundNotification {
    // the same to applicationWillEnterForeground:
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(open)
                                                 name: UIApplicationWillEnterForegroundNotification
                                               object:nil];

    //// the same to applicationWillResignActive
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(close)
                                                 name: UIApplicationWillResignActiveNotification
                                               object:nil];
}


- (void)open {
    NSLog(@"the same to applicationWillEnterForeground");
}

- (void)close {
    NSLog(@"the same to applicationWillResignActive");
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:applicationWillEnterForeground object:Nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:applicationWillResignActive object:Nil];
}

2: Use UIApplicationDelegatedelegate Method

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (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.
}



回答2:


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.)




回答3:


If 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.




回答4:


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:

applicationWillResignActive: (Called when leaving the foreground state.)
applicationWillEnterForeground: (Called when transitioning out of the background state.)

Or you can use NSNotificationCenter and the equivilent notifications:

UIApplicationWillEnterForegroundNotification
UIApplicationWillResignActiveNotification

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.



来源:https://stackoverflow.com/questions/30487732/stop-game-app-if-home-button-clicked-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!