Detect if the application in background or foreground in swift

68,942

Solution 1

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

Solution 2

Swift 3

  let state: UIApplicationState = UIApplication.shared.applicationState

            if state == .background {

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

                // foreground
            }

Solution 3

Swift 4

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

Solution 4

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

Solution 5

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
}
Share:
68,942
Mohammed Aboelwafa
Author by

Mohammed Aboelwafa

More learn , more experience.

Updated on December 14, 2021

Comments

  • Mohammed Aboelwafa
    Mohammed Aboelwafa over 2 years

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

  • Anbu.Karthik
    Anbu.Karthik almost 8 years
    @MohammedAboelwafa - I modifed check teh updated answer
  • Mohammed Aboelwafa
    Mohammed Aboelwafa almost 8 years
    Thanks @Anbu.Karthik
  • Saleem Khan
    Saleem Khan over 7 years
    can you share the complete code. Where we will test this function?
  • dimohamdy
    dimohamdy over 7 years
    @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
  • Saleem Khan
    Saleem Khan over 7 years
    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?
  • Amir Shabani
    Amir Shabani over 6 years
    Actually there are differences between background in our mind and ios's thoughts. see this link
  • bibscy
    bibscy almost 6 years
    @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?
  • Anbu.Karthik
    Anbu.Karthik almost 6 years
    @bibscy- based on your ?, the apple delegate method UIApplicationDidBecomeActive will call
  • user924
    user924 about 5 years
    it returns inactive even if app visible
  • user924
    user924 about 5 years
    it returns inactive even if app visible (in viewDidLoad)
  • Anbu.Karthik
    Anbu.Karthik about 5 years
    @user924 - is this possible to attach the video