I am using the user preferences to save the state of my game so that when a user goes away and comes back, it comes back at the same place they left. However, I am saving to this every turn. Is there a better way of doing this. Putting it in the applicationWillTerminate method maybe? If so, I'm not quite sure how to accomplish it there. Can anyone give any advice on this?
applicationWillTerminate is the proper place to save your game. What is the difficulty in performing the action there? You just need to save the game as you would from the other location in your application from which you are performing the action. You may need to store a reference to the controller (or other class) in the application delegate to be able to perform the save action.
I guess that's the problem I'm having. How do I get a reference to my class that runs the game into the application delegate? Is there an easy way to access the view controller from the application delegate?
Also can the applicationWillTerminate go into the view controller, or does it need to be in the application delegate?
I made it work by setting up the MainViewController as a class and a variable in the Application Delegate. Then once the MainViewController was established, I called a method I put into the Application Delegate to set that variable to "self" (meaning MainViewController instance). Finally, in the applicationWillTerminate method of the Application Delegate, I used that variable to call the save method in the MainViewController.
Perhaps this is difficult to follow without seeing the code, but does this sound overly complicated, or about right?
This thread has been closed by the system or the community team.
You may vote for any posts you find helpful, or search the Community for additional answers.
Method to save application state just before quit
Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.