Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 561
Nasir Avatar asked Jan 22 '15 06:01

Nasir


People also ask

What is inactive state in iOS?

inactive. The app is running in the foreground but isn't receiving events.

How do I check app status on Iphone?

Click on the iOS application from the left-hand side menu under the App store tab (see below screenshot). Here you will find the application publish status. If the application is in LIVE, then the status is “Ready for sale” otherwise it is in “waiting for review”/ “In review” / “Rejected” (if any rejection).

How do I know if an app is in the background or foreground iOS?

To detect if an iOS application is in background or foreground we can simply use the UIApplication just like we can use it to detect many other things like battery state, status etc. The shared. application state is an enum of type State, which consists of the following as per apple documentation.

What is inactive state iOS Swift?

Inactive - The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received. Active - The app is running in the foreground, and receiving events.


1 Answers

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 {}
like image 110
Josh Gafni Avatar answered Sep 23 '22 11:09

Josh Gafni