有什么方法可以检查iOS应用是否在后台?


Answers:


285

应用程序委托获取指示状态转换的回调。您可以基于此进行跟踪。

UIApplication中的applicationState属性也返回当前状态。

[[UIApplication sharedApplication] applicationState]

64
谢谢您-并且更加清晰,它是[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground。
播客

39
我认为[[UIApplication sharedApplication] applicationState] != UIApplicationStateActive更好,因为UIApplicationStateInactive几乎等同于处于后台...
JP Illanes


2
我发现,如果出于后台获取目的而使您的应用程序栩栩如生,则不会调用过渡回调。
Mike Asdf '16

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

这可以帮助您解决问题。

请参阅下面的评论-非活动状态是一种非常特殊的情况,可能意味着该应用程序正在启动中。根据您的目标,这可能对您意味着“背景”。


不错,很有帮助。工作
Saranjith

1
从文档:UIApplicationStateInactive-该应用程序在前台运行,但未收到事件。这可能是由于中断引起的,或者是因为应用正在过渡到后台或从后台过渡而来。
安迪·温斯坦

30

迅捷3

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

反之亦然吗?我可以检查== .inactive || == .active以查看它是否在前台(而不是在后台线程上打开)?
robertotomás19年

21

迅捷版:

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

8

如果您希望接收回调而不是“询问”应用程序状态,请在您的中使用以下两种方法AppDelegate

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


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

1
请务必注意,在applicationDidBecomeActive之前调用applicationWillEnterForeground。因此,从applicationWillEnterForeground内部检查applicationState将返回UIApplicationStateBackground。这让我有些失望。因此,我通过从applicationDidBecomeActive内部检查applicationState而不是(不正确地)从applicationWillEnterForeground内部检查applicationState中的UIApplicationStateBackground来使用上述解决方案的组合。
贾斯汀·多姆尼兹

我有相同的解决方案,它适用于需要其他线程/队列上的状态信息的情况。
naz

4

迅捷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

因为没有身体,所以要为迅捷4写信,为迅捷4人民写信
Shakeel Ahmed

该代码将检查您是否在后台运行该应用程序!假设您正在后台接收通知,并且想要做一些事情,例如,如果通知将在后台应用程序执行时收到通知,则执行一些操作
Shakeel Ahmed

3

迅捷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
        }

1

一个Swift 4.0扩展,使访问起来更加容易:

import UIKit

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

要从您的应用内访问:

let myAppIsInBackground = UIApplication.shared.isBackground

如果您正在寻找在各种状态信息(activeinactivebackground),你可以找到这里的苹果文档

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.