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

发布于 2024-12-18 10:37:43 字数 144 浏览 3 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-12-25 10:37:43

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

您可能需要考虑的值:

  • UIApplicationStateActive
  • UIApplicationStateInactive
  • UIApplicationStateBackground

例如

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

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

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

Values you may want to consider:

  • 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;
}
暮年 2024-12-25 10:37:43

在某些情况下,检查状态不起作用。

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

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

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

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

applicationDidFinishLaunching
applicationWillEnterForeground

:错误于:

applicationDidEnterBackground
applicationWillResignActive 

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.

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

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

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:

applicationDidFinishLaunching
applicationWillEnterForeground

and false in:

applicationDidEnterBackground
applicationWillResignActive 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文