28

As we all knows, if an iOS app is running foreground, then the app won't notify users when the remove notification come. Now In my app, I want to show alert to notify users that remote notification comes. How to judge if the app is running foreground or background? I have found the docs and searched stackoverflow.com and failed to find any thing about that. Thank you.

1
  • Sorry about my wrong spelling. Commented Nov 28, 2011 at 5:11

2 Answers 2

78

[UIApplication sharedApplication].applicationState will return current state, check it possible values and don't create unnecessary flags when you can use system features.

Values you may want to consider:

  • UIApplicationStateActive
  • UIApplicationStateInactive
  • UIApplicationStateBackground

e.g.

+(BOOL) runningInBackground
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    return state == UIApplicationStateBackground;
}

+(BOOL) runningInForeground
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    return state == UIApplicationStateActive;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, your answer is great!
Didn't know about that property - handy!
Possible values are: UIApplicationStateActive, UIApplicationStateInactive or UIApplicationStateBackground.
and also, UIApplicationStateActive
6

There are cases where checking the state does not work.

Here is one I ran into: If you try to use BT and it is disabled, iOS will bring up a dialog asking if the user would like to turn BT on. When this happens, the application state is not a reliable way to determine if your app is in the foreground.

First of all, you will get two applicationDidBecomeActive events - one (correctly) when the app appears and then another one (incorrectly) after the user presses the button in the dialog (while the iOS settings is the front-most app).

UIApplication.applicationState will say "Active" even though this is not the case (at least if you interpret "active" as being in the foreground, as was the original question).

Since you don't get willEnterForeground on first launch, the only reliable way of detecting if the app is visible or not (As far as I have been able to figure out) is to have a flag and then set it to true in:

applicationDidFinishLaunching
applicationWillEnterForeground

and false in:

applicationDidEnterBackground
applicationWillResignActive 

4 Comments

Thanks for your answer NJ, since the time you posted this, does this still appear reliable to you?
Yes, I am not aware of any cases where it does not work.
This will not work if you have a VoIP app. On device boot it is launched automatically, you only get applicationDidFinishLaunching and you will assume that it's in Foreground (because you will not get applicationWillEnterForeground), but actually it is in Background as correctly stated by applicationState. So Pavel Oganesyan is right, but you need to check both UIApplicationStateActive and UIApplicationStateInactive states for correct Foreground status.
Correct about voip app, sadly, seems no reliable way to determine foreground state...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.