Mohammed Aboelwafa
Mohammed Aboelwafa

Reputation: 1095

Detect if the application in background or foreground

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

Upvotes: 86

Views: 105555

Answers (6)

Mahdi Moqadasi
Mahdi Moqadasi

Reputation: 2479

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() {
}

Upvotes: 20

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

[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
}

Upvotes: 164

Hamish
Hamish

Reputation: 1745

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
}

Upvotes: 4

Ashu
Ashu

Reputation: 3523

Swift 4

let state = UIApplication.shared.applicationState
        if state == .background {
            print("App in Background")
        }else if state == .active {
            print("App in Foreground or Active")
        }

Upvotes: 8

dimohamdy
dimohamdy

Reputation: 3053

Swift 3

  let state: UIApplicationState = UIApplication.shared.applicationState

            if state == .background {

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

                // foreground
            }

Upvotes: 20

Atlandu
Atlandu

Reputation: 29

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.

Upvotes: 1

Related Questions