それを実装するには、次の 2 つの方法があります。
1: 使用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:UIApplicationDelegate
デリゲート メソッドを使用する
- (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.
}