iOS如何判断应用程序是运行前台还是后台? [英] iOS how to judge application is running foreground or background?

查看:645
本文介绍了iOS如何判断应用程序是运行前台还是后台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,如果iOS应用程序正在运行前台,那么应用程序将不会在删除通知到来时通知用户。现在在我的应用程序中,我想显示警报以通知用户远程通知到来。如何判断应用程序是运行前台还是后台?我找到了docs并搜索了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.

推荐答案

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

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

您可能要考虑的值:


  • UIApplicationStateActive

  • UIApplicationStateInactive

  • UIApplicationStateBackground

例如

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

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

这篇关于iOS如何判断应用程序是运行前台还是后台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆