How to determine app is in active state or inactive state ios?
Asked Answered
T

2

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.

Thornie answered 22/1, 2015 at 6:37 Comment(2)
Default App delegates in the template already provide what you need. Read the comments within the functions.Sevenfold
@Tchami How is that a duplicate if the question is about iOS?Patricia
R
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 {}
Ryanryann answered 22/1, 2015 at 6:41 Comment(5)
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 UIApplicationWillBecomeINActiveNotificationThornie
I updated my answer. You can use UIApplicationDidEnterBackgroundNotification. An app goes through background before becoming inactive.Ryanryann
Great answer :) Definitly found some useful stuff for everyday use in thereLyon
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.Thornie
You might find this helpful. #6651217. 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?Ryanryann
B
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

Butternut answered 22/1, 2015 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.