0

I have a SpriteKit/SwiftUI project.

Let's say my app gets terminated by iOS because of memory pressure, or some other similar problem caused by my app. Will my delegate life cycle methods be called, or not?

Specifically, I'm wondering if my AppDelegate's applicationWillTerminate(_:) method will be called. Will it be called regardless of the cause of the termination, or are there certain termination causes that will result in it not being called?

I saw this question, but the answer doesn't include any references or anything -- it's just a random claim that may or may not be accurate.

Thank you for your input!

West1
  • 1,430
  • 16
  • 27
  • 1
    This is clearly a duplicate of the linked question. That answer is correct. Is there some more to your question other than you don't believe the existing answer? (Have you tried building a small app and killing it to see what happens? The answer given is correct. In fact, I don't believe there are any cases where `applicationWillTerminate` *will* be called in modern version of iOS. While the docs suggest it might happen, I don't recall it ever actually happening.) – Rob Napier Sep 06 '21 at 15:33
  • 1
    Does this answer your question? [When an app crashes does either the 'applicationWillTerminate' or 'applicationDidEnterBackground' get called?](https://stackoverflow.com/questions/10221573/when-an-app-crashes-does-either-the-applicationwillterminate-or-applicationdi) – Alastar Sep 06 '21 at 15:41
  • (By "modern version" I mean since around iOS 7 or so. It's very possible it still does get called in some cases, such as system shutdown, but in practice there has been little reason to implement applicationWillTerminate since multitasking was added in iOS 4. Prior to that it was a very important method since it got called every time the user pressed the home button.) To your broader question, absolutely not. If your process receives a SIGKILL, it will run no more code, period. That's a Unix rule. – Rob Napier Sep 06 '21 at 15:42
  • The Apple documentation literally says to use `applicationWillTerminate` for the In App Purchases process, which you're welcome to learn about here: https://developer.apple.com/documentation/storekit/original_api_for_in-app_purchase/setting_up_the_transaction_observer_for_the_payment_queue. – West1 Sep 06 '21 at 15:44
  • @Alastar That's literally the question to which I linked in my post, dude. – West1 Sep 06 '21 at 15:45
  • @RobNapier `applicationWillTerminate` *is* being called. – West1 Sep 06 '21 at 15:48
  • In what situation is it being called? – Rob Napier Sep 06 '21 at 15:49
  • @RobNapier Well, when I swipe the app away (close the app), for one thing. – West1 Sep 06 '21 at 15:50
  • Interesting; I'm pretty sure that's a change (and a welcome one). See https://stackoverflow.com/questions/13386505/applicationwillterminate-not-getting-called-on-force-quit-of-ios-app for the older behavior (which matches my experience from the same time). I wonder if this was part of the move to scene lifecycles (iOS 13)? It doesn't change the fact that you're absolutely not promised to get this message and you cannot rely on it. But it's helpful to know that there are now some common cases where it does get called. – Rob Napier Sep 06 '21 at 16:10
  • 1
    Note specifically that the app won't get it (and are documented not to get it) when the app is suspended. "However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason." In that case the app is simply killed rather than bringing it back to the background state just to terminate it. – Rob Napier Sep 06 '21 at 16:11
  • If you have questions about when delegate methods are called, you can empirically confirm, e.g., https://stackoverflow.com/questions/68491641/if-i-lock-the-screen-or-the-iphone-starts-to-sleep-but-i-never-tab-out-of-the-a/68503861#68503861 – Rob Sep 06 '21 at 16:37
  • “`applicationWillTerminate` is being called ... when I swipe the app away (close the app), for one thing” ... In my experience, when you simply leave an app by jumping to the home screen or jumping to another app, `applicationWillTerminate` is not called (because it is not terminated yet, simply suspended). And as Rob Napier said, [the documentation](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate/) confirms this, that it is called only in very specific situations. – Rob Sep 06 '21 at 16:52

0 Answers0