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

ぃ、小莉子 提交于 2019-11-30 11:17:47
- (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 {

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!