To test for the state you can do something like:
[[UIApplication sharedApplication] applicationState]==UIApplicationStateInactive
or
[[UIApplication sharedApplication] applicationState]==UIApplicationStateActive
If you want to be notified you can do:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourselector:)
name:UIApplicationDidBecomeActiveNotification object:nil];
or
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourselector:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
You can do other notifications too (from https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/):
UIApplicationDidBecomeActiveNotification
UIApplicationDidChangeStatusBarFrameNotification
UIApplicationDidChangeStatusBarOrientationNotification
UIApplicationDidEnterBackgroundNotification
UIApplicationDidFinishLaunchingNotification
UIApplicationDidReceiveMemoryWarningNotification
UIApplicationProtectedDataDidBecomeAvailable
UIApplicationProtectedDataWillBecomeUnavailable
UIApplicationSignificantTimeChangeNotification
UIApplicationUserDidTakeScreenshotNotification
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationWillChangeStatusBarFrameNotification
UIApplicationWillEnterForegroundNotification
UIApplicationWillResignActiveNotification
UIApplicationWillTerminateNotification
UIContentSizeCategoryDidChangeNotification
If you want to use the app delegate, you can use:
- (void)applicationDidEnterBackground:(UIApplication *)application {}
or
- (void)applicationDidBecomeActive:(UIApplication *)application {}