Reputation: 475
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
Upvotes: 37
Views: 25432
Reputation: 1906
Swift 5 version:
import UIKit
let isForeground = UIApplication.shared.applicationState == .active
Upvotes: 19
Reputation: 92316
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.
Upvotes: 65
Reputation: 4762
Just to have a copy-paste code available for others:
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
//App is in foreground. Act on it.
}
Upvotes: 21