在 iOS 上收到通知时确定应用程序是否正在前台运行

发布于 2024-12-23 01:06:56 字数 155 浏览 3 评论 0原文

我想找到一种方法来查看哪些应用程序正在前台运行,或者当我的应用程序出现本地通知时是否显示主屏幕。例如,如果主屏幕或其他应用程序中有不同的操作,我希望有不同的操作。 我尝试使用processed和pid,但是pid是在应用程序启动时生成的,而不是最后一次使用应用程序时生成的。 有什么想法吗? 谢谢

I would like to find a way to see what app is running in foreground or if the home screen is displayed when a local notification from my app show up. For example i want to have different actions if there is in homescreen or in someone else app.
I tried to use processed and pid but the pid is generated hen the app starts and not the last time the app is used.
Any idea?
thanks

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

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

发布评论

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

评论(3

梦行七里 2024-12-30 01:06:56

推送通知文档 你可以阅读< code>[[UIApplication sharedApplication] applicationState] 当您收到通知以确定您的应用程序是否处于前台、非活动状态(它是可见的,但像WiFi 选择器位于前面)或位于后台。

As described in the push notification documentation you can read [[UIApplication sharedApplication] applicationState] when you receive the notification to determine whether your app is in foreground, inactive (it's visible but a dialog like the WiFi chooser is in front) or in background.

阳光下的泡沫是彩色的 2024-12-30 01:06:56

只是为了让其他人可以复制粘贴代码:

if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
    //App is in foreground. Act on it.
}

Just to have a copy-paste code available for others:

if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
    //App is in foreground. Act on it.
}
蓝色星空 2024-12-30 01:06:56

Swift 5 版本:

import UIKit
let isForeground = UIApplication.shared.applicationState == .active

Swift 5 version:

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