Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C: How to check if application is currently active (i.e. user is using it)?

I am implementing an APNS push for my app with the following behaviours

1) If app is not active i.e. in background or not launched yet:

User will receive a pop up message and on click will bring him/her to the 3rd tab in the app (see code below)

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    self.tabController.selectedIndex = 3;

}

2) If app is launch and currently active

No popup alert will be shown and the number of new notifications will show as a number on the 3rd tab

enter image description here

My question is how can I determine if the user's app is active? How and where can I implement the check so that I can set the 2 different behaviours?

like image 961
Zhen Avatar asked Jul 18 '11 08:07

Zhen


People also ask

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.

How do I find my OS version in Objective C?

In Objective-C, you need to check the system version and perform a comparison. [[NSProcessInfo processInfo] operatingSystemVersion] in iOS 8 and above.


1 Answers

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (application.applicationState == UIApplicationStateActive) {
        // update the tab bar item
    }
    else {
        self.tabController.selectedIndex = 3;
    }
}
like image 122
Stanislav Yaglo Avatar answered Oct 13 '22 07:10

Stanislav Yaglo