2
votes

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

1
If the user terminates your app by swiping it away, your app doesn’t get a chance to execute any code. - Paulw11
I don't think so, because you don't get the event that the app is force closed. - cldrr

1 Answers

3
votes

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")
}