开发者

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

开发者 https://www.devze.com 2023-03-20 20:07 出处:网络
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:

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 didReceiveRemote开发者_Go百科Notification:(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

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

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?


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if (application.applicationState == UIApplicationStateActive) {
        // update the tab bar item
    }
    else {
        self.tabController.selectedIndex = 3;
    }
}


My answer for new modern visitors...

Swift variable : Simply put this any where outside classes to check for app active state .

var isApplicationActive:Bool {
    get {return UIApplication.sharedApplication().applicationState == .Active}
}

so your code in swift will be

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    if isApplicationActive {
        //Change badge number ...
    } else {

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消