Is there any way to check if iOS app is in background?
Asked Answered
R

9

203

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

In:

locationManagerDidUpdateLocation {
    if(app is runing in background){
        do this
    }
}
Rawson answered 29/4, 2011 at 18:22 Comment(1)
Are you talking about the locationManager:didUpdateToLocation:fromLocation: method?Confection
B
298

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]
Brashear answered 29/4, 2011 at 18:24 Comment(4)
Thank you — and for additional clarity it's [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground.Kopaz
I think [[UIApplication sharedApplication] applicationState] != UIApplicationStateActive is better, as UIApplicationStateInactive is almost equivalent to be in background...Brooklynese
States are spelled out here: developer.apple.com/library/ios/documentation/uikit/reference/…Brahmani
I've found that the transition callbacks don't get called if your app is brought to life for background fetch purposes.Philo
C
184
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...

Cynthiacynthie answered 5/2, 2013 at 6:42 Comment(2)
Nice, Helpful answer. WorkedChain
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.Willey
E
38

Swift 3

    let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
    }
Ellsworth answered 24/1, 2017 at 5:27 Comment(1)
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)?Cistaceous
O
26

Swift version :

let state = UIApplication.shared.applicationState
if state == .Background {
    print("App in Background")
}
Oys answered 29/3, 2016 at 12:21 Comment(0)
C
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
    }
Commentary answered 24/4, 2018 at 11:30 Comment(3)
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.Damalas
because of no body write for swift 4 helping for the swift 4 peoplesCommentary
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 executeCommentary
P
9

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");
}
Politics answered 4/3, 2015 at 11:14 Comment(4)
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.Hourly
I had this same solution and it is suitable for cases where you need the state information on another thread/queue.Oliva
Doesn't seem to do anything in iOS 14, the methods don't get called for me.Khalsa
well, I wrote the post in 2015, probably tested it on iOS 8...Politics
T
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
        }
Tacye answered 11/9, 2019 at 7:53 Comment(0)
B
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.

Beware answered 22/6, 2018 at 17:58 Comment(0)
U
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 =)

Unruffled answered 10/11, 2022 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.