201

I want to check if the app is running in the background.

In:

locationManagerDidUpdateLocation {
    if(app is runing in background){
        do this
    }
}
mfaani
  • 33,269
  • 19
  • 164
  • 293
bobby grenier
  • 2,090
  • 2
  • 14
  • 11

9 Answers9

297

App delegate gets callbacks indicating state transitions. You can track it based on that.

Also the applicationState property in UIApplication returns the current state.

[[UIApplication sharedApplication] applicationState]
William Denniss
  • 16,089
  • 7
  • 81
  • 124
David Neiss
  • 8,161
  • 2
  • 20
  • 21
  • 66
    Thank you — and for additional clarity it's [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground. – podperson Jun 05 '12 at 16:01
  • 42
    I think `[[UIApplication sharedApplication] applicationState] != UIApplicationStateActive` is better, as UIApplicationStateInactive is almost equivalent to be in background... – JP Illanes Oct 28 '13 at 02:53
  • 6
    States are spelled out here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIApplicationState – Dan Rosenstark Apr 07 '14 at 18:00
  • 2
    I've found that the transition callbacks don't get called if your app is brought to life for background fetch purposes. – Mike Asdf Jul 06 '16 at 21:21
183
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
   //Do checking here.
}

This may help you in solving your problem.

See comment below - inactive is a fairly special case, and can mean that the app is in the process of being launched into the foreground. That may or may not mean "background" to you depending on your goal...

Andy Weinstein
  • 2,639
  • 3
  • 21
  • 32
Aswathy Bose
  • 4,279
  • 4
  • 32
  • 44
  • Nice, Helpful answer. Worked – Saranjith Nov 02 '17 at 03:56
  • 1
    From the docs: UIApplicationStateInactive - The app is running in the foreground but is not receiving events. This might happen as a result of an interruption or because the app is transitioning to or from the background. – Andy Weinstein Jan 07 '18 at 12:00
38

Swift 3

    let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
    }
Meet
  • 609
  • 6
  • 10
  • is the reverse also true? can I check == .inactive || == .active to see if it is in the foreground (as opposed to opening on background thread)? – roberto tomás Apr 27 '19 at 18:55
26

Swift version :

let state = UIApplication.shared.applicationState
if state == .Background {
    print("App in Background")
}
hannojg
  • 983
  • 8
  • 28
ioopl
  • 1,735
  • 19
  • 19
10

swift 5

let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
        //MARK: - if you want to perform come action when app in background this will execute 
        //Handel you code here
    }
    else if state == .foreground{
        //MARK: - if you want to perform come action when app in foreground this will execute 
        //Handel you code here
    }
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Apr 24 '18 at 11:51
  • because of no body write for swift 4 helping for the swift 4 peoples – Shakeel Ahmed Apr 24 '18 at 12:22
  • the code will check you state if the app in background! Suppose if you are receiving the notification in background and you want do some stuff like perform some action if notification receive when the app in background the inner code will execute – Shakeel Ahmed Apr 25 '18 at 07:00
8

If you prefer to receive callbacks instead of "ask" about the application state, use these two methods in your AppDelegate:

- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"app is actvie now");
}


- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"app is not actvie now");
}
klimat
  • 24,711
  • 7
  • 63
  • 70
  • 1
    It's important to note that applicationWillEnterForeground is called before applicationDidBecomeActive. Therefore, checking applicationState from within applicationWillEnterForeground will return UIApplicationStateBackground. This threw me off for a bit. So, I used a combination of the above solutions by checking applicationState from within applicationDidBecomeActive instead of (incorrectly) checking applicationState for UIApplicationStateBackground from within applicationWillEnterForeground. – Justin Domnitz Nov 13 '15 at 14:02
  • 1
    I had this same solution and it is suitable for cases where you need the state information on another thread/queue. – naz Oct 06 '17 at 09:41
  • Doesn't seem to do anything in iOS 14, the methods don't get called for me. – Supertecnoboff Mar 15 '21 at 08:37
  • well, I wrote the post in 2015, probably tested it on iOS 8... – klimat Mar 15 '21 at 22:30
4

Swift 4+

let appstate = UIApplication.shared.applicationState
        switch appstate {
        case .active:
            print("the app is in active state")
        case .background:
            print("the app is in background state")
        case .inactive:
            print("the app is in inactive state")
        default:
            print("the default state")
            break
        }
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Raj Kumar
  • 49
  • 1
2

A Swift 4.0 extension to make accessing it a bit easier:

import UIKit

extension UIApplication {
    var isBackground: Bool {
        return UIApplication.shared.applicationState == .background
    }
}

To access from within your app:

let myAppIsInBackground = UIApplication.shared.isBackground

If you are looking for information on the various states (active, inactive and background), you can find the Apple documentation here.

Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35
CodeBender
  • 35,668
  • 12
  • 125
  • 132
1

Thanks to Shakeel Ahmed this is what worked for me in Swift 5

switch UIApplication.shared.applicationState {
case .active:
    print("App is active")
case .inactive:
    print("App is inactive")
case .background:
    print("App is in background")
default:
    return
}

I hope it helps someone =)

Linus
  • 423
  • 5
  • 12