15

I have some code in my AppDelegate’s applicationWillTerminate: method but I don’t know how to test if it works. Using Xcode to stop the simulator does not trigger it.

How do I test the code in applicationWillTerminate:?

Please note that this is specific to the simulator and not the device.

duci9y
  • 4,128
  • 3
  • 26
  • 42
  • Maybe kill it. Since iOS 4, apps don't terminate upon being quit, they are just suspended. –  Dec 29 '12 at 07:42
  • The code in that method should probably be in `applicationDidEnterBackground:` - what does it do? – jrturton Dec 29 '12 at 07:52
  • It synchronises the NSUserDefaults. I have put it in `applicationDidEnterBackground:` too. – duci9y Dec 29 '12 at 07:56
  • Possible duplicate of [applicationWillTerminate not getting called on force quit of iOS app](http://stackoverflow.com/questions/13386505/applicationwillterminate-not-getting-called-on-force-quit-of-ios-app) – Max MacLeod Dec 07 '15 at 16:50

3 Answers3

18

The simulator doesn't send applicationWillTerminate: because the springboard isn't sending you the kill code (SIGABRT), LLDB is. To trigger those delegate methods, you have to use the Home Button command (⌘+⇧+H), then pull up the multitasking drawer and kill your application from there. Your debugging session will immediately terminate, and the delegate method will fire successfully.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
2

The only way it worked with me was after I did the following steps:

  1. Edit the target schema
  2. Select Run tap
  3. Choose Wait for the execution to be launched as a launch option
  4. Run the app from Xcode
  5. Open the app on the simulator/device
  6. double click the home button
  7. remove the app from background

Wait for the execution to be launched

pkamb
  • 33,281
  • 23
  • 160
  • 191
Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62
1

In iOS 9.1 (perhaps before) this only seems to work if you double click home and drag the app up so it exits. Doing it from the desktop doesn't fire this event, it has to happen while the app is open in the simulator.

enter image description here

Guy Lowe
  • 2,115
  • 1
  • 27
  • 37
  • This did it for me, although it is way harder to double tap home on the Simulator than it should be. 90% of the Simulator would background the app. It's much easier to just ⇧⌘H twice in a row. – rob5408 Mar 16 '18 at 17:43