Stop Game App if Home Button clicked iOS [closed]

2019-09-19 11:02发布

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.

4条回答
你好瞎i
2楼-- · 2019-09-19 11:28

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.
}
查看更多
贼婆χ
3楼-- · 2019-09-19 11:33

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.

查看更多
\"骚年 ilove
4楼-- · 2019-09-19 11:45

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

查看更多
做自己的国王
5楼-- · 2019-09-19 11:52

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.

查看更多
登录 后发表回答