Laurens Artapps
Laurens Artapps

Reputation: 48

(iOS) Cancelling all local notifications when app is no longer running

I'm working on an app that is too complex to fully explain, so to support my question I will provide a very basic example of what I'm trying to achieve.

Say I have an app that shows a timer of 10 minutes. If a button is pressed, the timer runs down to 0. When the app is closed (not terminated), I want to notify the user of this by a local notification.

When the app gets terminated, the timer obviously stops as well. However, the local notification would still be set up and fired eventually.

I don't want this to happen. I only want to show the notification if the app is still running in the background. If the app gets terminated by the user or by a crash, the notification should not appear.

Is there any way of achieving this?

Thanks in advance guys.

Laurens

Upvotes: 2

Views: 98

Answers (1)

Francesco Deliro
Francesco Deliro

Reputation: 3939

You can check the state of your app where you setup and show your local notification:

Swift 3

let state = UIApplication.shared.applicationState
if state == .background {
    print("App in Background")
}

Upvotes: 3

Related Questions