Search code examples
objective-cnsuserdefaultsappdelegatehome-button

How to save a NSUserDefault using applicationDidEnterBackground in the appdelegate


Seems a simple question, but I'm not able to find an answer. I have some user information that I want to save locally using NSUserDefaults whenever the user presses the home button. I've read that I should put this in the appdelegate classes applicationDidEnterBackground method. But how do I make the actual data to be saved (in the view controllers) available to the applicationDidEnterBackground method? Thanks for the help.


Solution

  • You have the right idea, but the code doesn't have to be in the app delegate. You can observe the notification that is sent when the application enters the background from anywhere and save your data when this notification is received.