9

I need to know when an App is in Foreground, it is in active state or inactive state ?

If my App is in inactive state I need to fire the Logout Protocol and destroy the current user's session,

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"App is not active logout success");
}

Is there any appDelegate method which tell me that app is in inactive state, any code example will help me a lot.

If need work with "NSNotificationCenter", in which class can I add the code and who will be observer.

esqew
  • 42,425
  • 27
  • 92
  • 132
Nasir
  • 1,617
  • 2
  • 19
  • 34

2 Answers2

8

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 {}
Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
  • Hey Thanks, you have solved my problem 50%, I need a little bit more help, I need the something which is opposite to `UIApplicationDidBecomeActiveNotification` like `UIApplicationDidBecomeInActiveNotification` or `UIApplicationWillBecomeINActiveNotification` – Nasir Jan 22 '15 at 06:59
  • I updated my answer. You can use UIApplicationDidEnterBackgroundNotification. An app goes through background before becoming inactive. – Josh Gafni Jan 22 '15 at 07:03
  • 1
    Great answer :) Definitly found some useful stuff for everyday use in there – Gil Sand Jan 22 '15 at 07:11
  • Yes I know these things, but none one of them is worthful to me. Let me Tell u scene, 1) App launches (**App is always in Foreground never goes to background**) 2) suppose for time being (say _60sec_) App does not receive any event from user. 3) It means after _60sec_ App comes to **inactive** state. 4) After _60sec_ (in **inactive state**) i m going to fire the logout protocol. – Nasir Jan 22 '15 at 07:14
  • You might find this helpful. http://stackoverflow.com/questions/6650717/when-an-ios-application-goes-to-the-background-are-lengthy-tasks-paused. You can run background task in background for 10 minutes. Perhaps you set a timer for 60 seconds and then call your logout method , unless the app becomes active again? – Josh Gafni Jan 22 '15 at 07:39
0

Please refer this Apple Doc :App Life Cycle

applicationDidBecomeActive:—Lets your app know that it is about to become the foreground app. Use this method for any last minute preparation.

applicationWillResignActive:—Lets you know that your app is transitioning away from being the foreground app. Use this method to put your app into a quiescent state.

applicationWillEnterForeground:—Lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active.

applicationWillTerminate:—Lets you know that your app is being terminated. This method is not called if your app is suspended

Kannan Prasad
  • 1,796
  • 22
  • 27