bobby grenier
bobby grenier

Reputation: 2130

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

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

In:

locationManagerDidUpdateLocation {
    if(app is runing in background){
        do this
    }
}

Upvotes: 205

Views: 104746

Answers (9)

Linus Karlsson
Linus Karlsson

Reputation: 546

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 =)

Upvotes: 1

ioopl
ioopl

Reputation: 1745

Swift version :

let state = UIApplication.shared.applicationState
if state == .Background {
    print("App in Background")
}

Upvotes: 26

Shakeel Ahmed
Shakeel Ahmed

Reputation: 6051

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
    }

Upvotes: 10

Raj Kumar
Raj Kumar

Reputation: 49

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
        }

Upvotes: 4

CodeBender
CodeBender

Reputation: 36670

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.

Upvotes: 2

Aswathy Bose
Aswathy Bose

Reputation: 4289

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...

Upvotes: 184

Meet
Meet

Reputation: 619

Swift 3

    let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
    }

Upvotes: 39

David Neiss
David Neiss

Reputation: 8237

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]

Upvotes: 298

klimat
klimat

Reputation: 25011

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");
}

Upvotes: 9

Related Questions