How to identify application in active state or inactive state of ios?

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

If my application is in an inactive state, I need to start the exit protocol and destroy the current user session,

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

Is there any appDelegate method that tells me that the application is in an inactive state, any sample code will help me a lot.

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

+5
source share
2 answers

To check the status, you can do something like:

 [[UIApplication sharedApplication] applicationState]==UIApplicationStateInactive 

or

 [[UIApplication sharedApplication] applicationState]==UIApplicationStateActive 

If you want to receive notifications, you can:

 [[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 also make other notifications (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 application delegate, you can use:

 - (void)applicationDidEnterBackground:(UIApplication *)application {} 

or

 - (void)applicationDidBecomeActive:(UIApplication *)application {} 
+7
source

Please refer to this Apple Doctor: Application Life Cycle

applicationDidBecomeActive : - Lets your application know that it should become a priority application. Use this method to prepare at the last minute.

applicationWillResignActive : - Lets you know that your application is moving from a foreground application. Use this method to put the application at rest.

applicationWillEnterForeground : - You know that your application is moving from the background and back to the foreground, but it is not yet activated.

applicationWillTerminate : - You know your application is ending. This method is not called if your application is paused.

0
source

Source: https://habr.com/ru/post/1211684/


All Articles