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.

progrmr
  • 75,956
  • 16
  • 112
  • 147
Smeegol
  • 2,014
  • 4
  • 29
  • 44

2 Answers2

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;
}
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
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 
N.J.
  • 679
  • 7
  • 13
  • Thanks for your answer NJ, since the time you posted this, does this still appear reliable to you? – Bradley Thomas Nov 14 '14 at 01:00
  • 1
    Yes, I am not aware of any cases where it does not work. – N.J. Nov 15 '14 at 06:50
  • 2
    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. – Shebuka Jan 14 '15 at 11:38
  • Correct about voip app, sadly, seems no reliable way to determine foreground state... – Ethan Long Jul 13 '17 at 06:52