Can I get message when I show UIAlertView

2019-01-15 16:59发布

问题:

I want to get message when system show UIAlertView so I can pause my game.

Anyone know how to figure out that?

The UIAlertView is not controlled by myself.

回答1:

Application delegate's applicationWillResignActive: will be called on interrupts. You can handle the pause there or you can even listen to the UIApplicationWillResignActiveNotification in your view controller and pause the game there.

You can look at this part of the iOS Application Guide that details the life cycle of the application and state transitions.



回答2:

A system alert is normally displayed in its own UIWindow. Install handlers for the UIWindowDidBecomeVisibleNotification and UIWindowDidBecomeHiddenNotification notifications to track when a UIWindow becomes visible and hidden respectively:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(aWindowBecameVisible:)
                                              name:UIWindowDidBecomeVisibleNotification
                                            object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(aWindowBecameHidden:)
                                              name:UIWindowDidBecomeHiddenNotification
                                            object:nil];

In the handlers, grab the UIWindow that changes state from the object property of the notification:

- (void)aWindowBecameVisible:(NSNotification *)notification
{
    UIWindow *theWindow = [notification object];
    NSLog(@"Window just shown: %@", theWindow);
}

- (void)aWindowBecameHidden:(NSNotification *)notification
{
    UIWindow *theWindow = [notification object];
    NSLog(@"Window just hidden: %@", theWindow);
}

Finally, check that theWindow contains a subview of type UIAlertView.



回答3:

If your UIAlertView is from Third party app (not from your app) then you can implement below delegate methods to pause and resume game.

To Pause game

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

To Resume game

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

For example if you receive call or SMS you can use above delegate to pause/resume game.



回答4:

Just make this:

- (void)applicationWillResignActive:(UIApplication *)application {
//pause
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
//resume
}