要测试状态,您可以执行以下操作:
[[UIApplication sharedApplication] applicationState]==UIApplicationStateInactive
或者
[[UIApplication sharedApplication] applicationState]==UIApplicationStateActive
如果您想收到通知,您可以执行以下操作:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourselector:)
name:UIApplicationDidBecomeActiveNotification object:nil];
或者
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourselector:)
name:UIApplicationDidEnterBackgroundNotification object:nil];
你也可以做其他通知(来自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
如果要使用应用委托,可以使用:
- (void)applicationDidEnterBackground:(UIApplication *)application {}
或者
- (void)applicationDidBecomeActive:(UIApplication *)application {}