有没有办法检查iOS应用程序是否在后台运行?

202

我想检查应用程序是否在后台运行。

在:

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

你是在谈论 locationManager:didUpdateToLocation:fromLocation: 方法吗? - Black Frog
9个回答

297

应用程序委托(App Delegate)会得到回调,指示状态转换。您可以基于此跟踪它。

此外,UIApplication中的applicationState属性返回当前状态。

[[UIApplication sharedApplication] applicationState]

66
谢谢 - 为了更清晰,它是[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground。 - podperson
42
我认为[[UIApplication sharedApplication] applicationState] != UIApplicationStateActive更好,因为UIApplicationStateInactive几乎等同于处于后台... - JP Illanes
6
这里列出了各个状态的拼写:https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIApplicationState - Dan Rosenstark
2
我发现如果你的应用程序是为了后台获取目的而被唤醒,转换回调函数将不会被调用。 - Mike Asdf

184
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
   //Do checking here.
}

这可能会帮助您解决问题。

请参见下面的评论 - “inactive”是一个相当特殊的情况,可能意味着应用程序正在被启动到前台。这可能或可能不意味着“后台”,具体取决于您的目标...


很好的、有帮助的回答。起作用了。 - Saranjith
1
从文档中得知:UIApplicationStateInactive - 应用程序正在前台运行,但未接收事件。这可能是由于中断或应用程序正在从后台转换到前台或从前台转换到后台。 - Andy Weinstein

38

Swift 3

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

反过来也是吗?我可以检查 == .inactive || == .active 来查看它是否在前台(而不是在后台线程上打开)吗? - roberto tomás

26

Swift版本:

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

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
    }

1
虽然这段代码可能回答了问题,但是提供关于为什么和/或如何回答问题的额外上下文可以提高其长期价值。 - rollstuhlfahrer
由于没有人为Swift 4编写帮助文档,这给Swift 4的开发者带来了困扰。 - Shakeel Ahmed
代码将会检查你的状态是否在后台运行!假设你正在后台收到通知,并且你想要做一些事情,比如在应用程序在后台接收通知时执行某些操作,内部代码将会被执行。 - Shakeel Ahmed

9
如果您更喜欢接收回调而不是“询问”应用程序状态,请在AppDelegate中使用以下两种方法:
- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"app is actvie now");
}


- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"app is not actvie now");
}

1
需要注意的是,applicationWillEnterForeground在applicationDidBecomeActive之前被调用。因此,在applicationWillEnterForeground中检查applicationState将返回UIApplicationStateBackground。这让我有点困惑。所以,我使用了上述解决方案的组合,而是在applicationDidBecomeActive中检查applicationState,而不是在applicationWillEnterForeground中(错误地)检查UIApplicationStateBackground的applicationState。 - Justin Domnitz
1
我曾经有过同样的解决方案,它适用于需要在另一个线程/队列上获取状态信息的情况。 - naz
在iOS 14中似乎没有任何作用,这些方法对我来说没有被调用。 - Supertecnoboff
好的,我在2015年写了这篇文章,可能在iOS 8上进行了测试... - klimat

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
        }

2
一个用于简化访问的 Swift 4.0 扩展:
import UIKit

extension UIApplication {
    var isBackground: Bool {
        return UIApplication.shared.applicationState == .background
    }
}

从您的应用程序内访问:
let myAppIsInBackground = UIApplication.shared.isBackground

如果您正在寻找有关各种状态(activeinactivebackground)的信息,您可以在苹果文档中找到。

1
感谢Shakeel Ahmed,以下是在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
}

我希望这能对某些人有所帮助 =)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接