ios view içerisinden uygulamanın arkaplana düşmesini yakalama

merhaba arkadaşlar,
ios programlamada uygulamanın ön planda mı yoksa arka planda mı olduğunu AppDelegate içerisinde yakalamak zorunda değiliz. istersek uygulamanın bu durumlarını yakalayabileceğimiz observer’ları ilgili view içerisine ekleyebiliriz. aşağıdaki örneğimde bir adet timer nesnesi var. uygulama arka plana düştüğünde timer‘ı durduruyorum ön plana geldiğinde tekrar başlatıyorum.

 
-(void)viewWillAppear:(BOOL)animated{
 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
 
-(void)viewWillDisappear:(BOOL)animated{
 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
 
}
 
-(void)appDidEnterBackground
{
    [self stopTimer];
}
 
-(void)appDidBecomeActive
{
    [self startTimer];
}
 
- (void) startTimer {
    _gameTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES];
}
- (void) stopTimer {
    [_gameTimer invalidate];
}
 
- (void) timerTicked:(NSTimer *) timer {
}