[英]Determine if an app is running in the foreground when a notification is received on iOS
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.
我尝试使用处理过的和 pid 但 pid 是在应用程序启动时生成的,而不是最后一次使用应用程序时生成的。 Any idea?
任何的想法? thanks
谢谢
如推送通知文档中所述,您可以在收到通知时阅读[[UIApplication sharedApplication] applicationState]
以确定您的应用程序是在前台、非活动状态(它是可见的,但像 WiFi 选择器这样的对话框在前面)还是在后台。
Just to have a copy-paste code available for others:只是为了让其他人可以使用复制粘贴代码:
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
//App is in foreground. Act on it.
}
Swift 5 version:斯威夫特 5 版本:
import UIKit
let isForeground = UIApplication.shared.applicationState == .active
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.