-1

I would like to send a notification when my app is terminated. Is this possible using background processes to send a message as the app is killed? I'm using swift.

Thanks, Rob.

Rob Brew
  • 51
  • 4
  • This is very unreliable. I think you can detect when the app is _about_ to be terminated, but there is practically no time to do anything in that time frame. What are you thinking of doing? – George Nov 30 '19 at 00:47

3 Answers3

1

No. If your app is backgrounded, it is suspended. It is not running. If it is now terminated, it doesn’t get brought back to a running state and sent a message of some sort. It just dies silently in its sleep.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • What about what [@Andres Gomez](https://stackoverflow.com/a/59112315/9607863) stated? I know it's not at all reliable, but it is there for _very_ quick actions. – George Nov 30 '19 at 00:53
  • @George_E The quickness is not relevant here. If the app is suspended, it will not be made active just to terminate it. Background apps spend most of their time suspended. Force quit will explicitly not execute applicationWillTerminate. – Rob Napier Nov 30 '19 at 02:41
0

you can use this function in your AppDelegate

func applicationWillTerminate(_ application: UIApplication) {
        bgTask = application.beginBackgroundTask(withName:"appWillEnd", expirationHandler: {() -> Void in
            // Do something to stop our background task or the app will be killed

        })

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

I hope that you can use it

Andres Gomez
  • 458
  • 3
  • 12
  • 1
    Don't begin background tasks without ending them at some point. If you don't do this, the OS won't just suspend your app, but will unceremoniously kill it. – Rob Nov 30 '19 at 02:05
  • 1
    This function is rarely called. You have to be in a background, but not suspended, state at a time when the OS, but not the user, decides to terminate the app (e.g. low memory). It happens, but it's a pretty unusual situation. Force-quit will not run this method, and in most low-memory situations you're already in suspended state. – Rob Napier Nov 30 '19 at 02:40
  • @RobNapier I don't think you're right about that. To my knowledge, `applicationWillTerminate` is always called when the user force-quits the app. I just confirmed this on my iOS 12 device and iOS 13 in Simulator. It's possible that this has changed in newer versions of iOS. But the Apple docs about this method don't say anything that indicates it no longer runs when force-quitting. – peacetype Dec 05 '21 at 01:16
0

The best you can reasonably hope for is to have the OS tell you when the user is leaving the app. E.g. you can implement applicationDidEnterBackground(_:) or applicationWillResignActive(_:) (or if using iOS 13 scene delegate, implement sceneDidEnterBackground(_:) or sceneWillResignActive(_:)).

For more information, see Preparing Your UI to Run in the Background or About the Background Execution Sequence.

Rob
  • 415,655
  • 72
  • 787
  • 1,044