80

is there any way to know the state of my application if it is in background mode or in foreground . Thanks

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Mohammed Aboelwafa
  • 1,035
  • 1
  • 9
  • 13
  • Don't know the exact thing, but you will get call when application enters in background at `func applicationDidEnterBackground(application: UIApplication) { }` in appDelegate – Sanchit Kumar Singh Aug 16 '16 at 10:08

6 Answers6

155

[UIApplication sharedApplication].applicationState will return current state of applications such as:

  • UIApplicationStateActive
  • UIApplicationStateInactive
  • UIApplicationStateBackground

or if you want to access via notification see UIApplicationDidBecomeActiveNotification

Swift 3+

let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
    // background
} else if state == .active {
    // foreground
}

switch UIApplication.shared.applicationState {
    case .background, .inactive:
        // background
    case .active:
        // foreground
    default:
        break
}

Objective C

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive) {
    // background
} else if (state == UIApplicationStateActive) {
    // foreground
}
Alexander MacLeod
  • 2,026
  • 3
  • 14
  • 22
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • 1
    @MohammedAboelwafa - I modifed check teh updated answer – Anbu.Karthik Aug 16 '16 at 10:11
  • Thanks @Anbu.Karthik – Mohammed Aboelwafa Aug 16 '16 at 10:52
  • @MohammedAboelwafa My app has a SegmentedControl on top of table view. If the app is in foreground, then goes back in background(user taps home button) and he taps again on the app after 2 days. Where should I check for the state of the app, ViewDidLoad, was initialized 2 days ago, viewWillAppear will not be called. So how do I get to check? – bibscy May 28 '18 at 19:11
  • @bibscy- based on your ?, the apple delegate method **[UIApplicationDidBecomeActive](https://stackoverflow.com/questions/3639859/handling-applicationdidbecomeactive-how-can-a-view-controller-respond-to-the)** will call – Anbu.Karthik May 29 '18 at 04:19
  • it returns inactive even if app visible – user924 May 02 '19 at 20:04
  • @user924 - is this possible to attach the video – Anbu.Karthik May 06 '19 at 04:30
18

Swift 3

  let state: UIApplicationState = UIApplication.shared.applicationState

            if state == .background {

                // background
            }
            else if state == .active {

                // foreground
            }
dimohamdy
  • 2,917
  • 30
  • 28
  • can you share the complete code. Where we will test this function? – Saleem Khan Jan 31 '17 at 06:48
  • @SaleemKhan in any function you want to use it some time you want to know app in foreground to take photo using camera or not – dimohamdy Jan 31 '17 at 09:44
  • I want to check if the app is running in the background and user click on the notification message then log out the app. So I want to you please confirm to me the app is the view from the background or not? – Saleem Khan Jan 31 '17 at 10:26
  • Actually there are differences between background in our mind and ios's thoughts. see this [link](https://stackoverflow.com/a/9508679/4360116) – Amir Shabani Sep 16 '17 at 10:03
  • it returns inactive even if app visible (in viewDidLoad) – user924 May 02 '19 at 20:05
12

Use these observers in viewDidload of your UIViewController:

let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
nc.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

and methods:

@objc func appMovedToBackground() {    
}

@objc func appMovedToForeground() {
}
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52
8

Swift 4

let state = UIApplication.shared.applicationState
        if state == .background {
            print("App in Background")
        }else if state == .active {
            print("App in Foreground or Active")
        }
Ashu
  • 3,373
  • 38
  • 34
3

If somebody wants it in swift 3.0

switch application.applicationState {
    case .active:
        //app is currently active, can update badges count here
        break
    case .inactive:
        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
        break
    case .background:
        //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
        break
    default:
        break
    }

for swift 4

switch UIApplication.shared.applicationState {
case .active:
    //app is currently active, can update badges count here
    break
case .inactive:
    //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
    break
case .background:
    //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    break
default:
    break
}
Hamish
  • 1,685
  • 22
  • 37
1

you can add a boolean when the application enter in background or enter in foreground. You have this information by using the App delegate.

According the Apple documentation, maybe you can also use the mainWindow property of your Application or the active status property of the app.

NSApplication documentation

Discussion The value in this property is nil when the app’s storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.

Atlandu
  • 29
  • 5