![](/img/trans.png)
[英]Execute few lines of code when received silent push notification and app is running in background, ios
[英]Determine if an app is running in the foreground when a notification is received on iOS
我想找到一種方法來查看哪些應用程序在前台運行,或者當我的應用程序顯示本地通知時是否顯示主屏幕。 例如,如果在主屏幕或其他應用程序中,我想有不同的操作。 我嘗試使用處理過的和 pid 但 pid 是在應用程序啟動時生成的,而不是最后一次使用應用程序時生成的。 任何的想法? 謝謝
如推送通知文檔中所述,您可以在收到通知時閱讀[[UIApplication sharedApplication] applicationState]
以確定您的應用程序是在前台、非活動狀態(它是可見的,但像 WiFi 選擇器這樣的對話框在前面)還是在后台。
只是為了讓其他人可以使用復制粘貼代碼:
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
//App is in foreground. Act on it.
}
斯威夫特 5 版本:
import UIKit
let isForeground = UIApplication.shared.applicationState == .active
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.