Event Handler when app closes?

I want to write to a preferences file when my iOS app closes (when stopped in debugger or when user taps home button). The logical place would be a Close event handler in the App class. But the iOS App class doesn’t have a Close event. The Close events in views and controls seem to never fire when the app closes. I’m saving LOTS of preferences and want to save only when the app closes. Any ideas?

You could use the Deactivate event of your view. It is fired when the view changes or the user closes your app. If you set a flag before changing views then you can test it in the event and react accordingly.

Thanks for the response, Jason. The Deactivate event for the active view does not fire when the app is closed in the debugger. It might work on a real device when the home button is pressed, but I don’t yet have the iOS license. The problem is I have LOTS of views. I would have to put my preferences code in the Deactivate event on ALL of the views. It would be nice to have a single event that fires when the app closes. I may have to rethink my preferences. I’m currently writing everything to a text file. Using a database and updating whenever a preference changes might be a better way of doing it.

Thanks!

In most cases you should be able to write preferences every time they change, it shouldn’t have too much overhead. If you expect a LOT of changes you may have to work around with some other solution. The reason there’s no app.close event is because iOS doesn’t actually have a quit/close event because apple considers iOS apps to never actually quit.
Apps must be prepared for termination to happen at any time and should not wait to save user data or perform other critical tasks.

Further, they state: “Suspended apps receive no notification when they are terminated; the system kills the process and reclaims the corresponding memory.”

applicationDidEnterBackground: may be of more use to you, but again Apple recommends against saving data during this procedure. You only have five seconds.

Tim is spot on, your app should be ready to be terminated for a number of reasons, at any time. iOS apps are not like OS X apps, especially in this regard.

Thanks Tim. Makes sense. I’m now saving the prefs as they change. Saving the values of around 75 properties is not really that much overhead. When coding in Obj-C, I use NSUserDefaults and call Synchronize from applicationDidEnterBackground. I’m sure we will soon see some declare libraries emerge that address these things. I’m not proficient at using declares, but this might be a good time to learn.

You don’t need to call synchronize on NSUserDefaults.

[quote=154096:@David Fleming]Thanks for the response, Jason. The Deactivate event for the active view does not fire when the app is closed in the debugger. It might work on a real device when the home button is pressed, but I don’t yet have the iOS license. The problem is I have LOTS of views. I would have to put my preferences code in the Deactivate event on ALL of the views. It would be nice to have a single event that fires when the app closes. I may have to rethink my preferences. I’m currently writing everything to a text file. Using a database and updating whenever a preference changes might be a better way of doing it.

Thanks![/quote]

in the simulator, you can do what the Home button does by pressing shift-command-H. That is what will happen on the device when the app is put away. Note that normally, an app does not quit. It is simply frozen in the back.

Incidentally, it may happen that on the device, you need to actually quit an app. For instance when I install a new font, Pages needs to be quit in order to refresh it’s font menu.

  • Hold the sleep/wake button on top until you get the “Slide to power off” dialog. Don’t slide.
  • Hold Home for about six seconds ; the app quits and you are back at the main screen.