ios iOS如何判断应用程序是前台运行还是后台运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8291192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:37:03  来源:igfitidea点击:

iOS how to judge application is running foreground or background?

iosforeground

提问by Smeegol

As we all knows, if an iOS app is running foreground, then the app won't notify users when the remove notification come. Now In my app, I want to show alert to notify users that remote notification comes. How to judge if the app is running foreground or background? I have found the docs and searched stackoverflow.com and failed to find any thing about that. Thank you.

众所周知,如果一个iOS应用程序在前台运行,那么当删除通知到来时,该应用程序不会通知用户。现在在我的应用程序中,我想显示警报以通知用户远程通知来了。如何判断应用是前台运行还是后台运行?我找到了文档并搜索了 stackoverflow.com,但没有找到任何相关信息。谢谢你。

回答by Pavel Oganesyan

[UIApplication sharedApplication].applicationStatewill return current state, check it possible values and don't create unnecessary flags when you can use system features.

[UIApplication sharedApplication].applicationState将返回当前状态,检查它可能的值,并且在您可以使用系统功能时不要创建不必要的标志。

Values you may want to consider:

您可能需要考虑的值:

  • UIApplicationStateActive
  • UIApplicationStateInactive
  • UIApplicationStateBackground
  • UIApplicationStateActive
  • UIApplicationStateInactive
  • UIApplicationStateBackground

e.g.

例如

+(BOOL) runningInBackground
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    return state == UIApplicationStateBackground;
}

+(BOOL) runningInForeground
{
    UIApplicationState state = [UIApplication sharedApplication].applicationState;
    return state == UIApplicationStateActive;
}

回答by N.J.

There are cases where checking the state does not work.

有些情况下检查状态不起作用。

Here is one I ran into: If you try to use BT and it is disabled, iOS will bring up a dialog asking if the user would like to turn BT on. When this happens, the application state is not a reliable way to determine if your app is in the foreground.

这是我遇到的一个问题:如果您尝试使用 BT 并且它被禁用,iOS 会弹出一个对话框,询问用户是否要打开 BT。发生这种情况时,应用程序状态不是确定您的应用程序是否在前台的可靠方法。

First of all, you will get two applicationDidBecomeActive events - one (correctly) when the app appears and then another one (incorrectly) after the user presses the button in the dialog (while the iOS settings is the front-most app).

首先,您将获得两个 applicationDidBecomeActive 事件 - 一个(正确)在应用程序出现时,然后另一个(错误)在用户按下对话框中的按钮后(而 iOS 设置是最前面的应用程序)。

UIApplication.applicationState will say "Active" even though this is not the case (at least if you interpret "active" as being in the foreground, as was the original question).

UIApplication.applicationState 会说“Active”,即使情况并非如此(至少如果您将“active”解释为在前台,就像最初的问题一样)。

Since you don't get willEnterForeground on first launch, the only reliable way of detecting if the app is visible or not (As far as I have been able to figure out) is to have a flag and then set it to true in:

由于您在第一次启动时没有得到 willEnterForeground,因此检测应用程序是否可见的唯一可靠方法(据我所知)是有一个标志,然后将其设置为 true:

applicationDidFinishLaunching
applicationWillEnterForeground

and false in:

和错误:

applicationDidEnterBackground
applicationWillResignActive