Question

For a project I'm working on, I would like to save a few bytes of data to the users iPhone between launches of the application. I would like to do this so I can save some state and a few important numbers when the user terminates the app. I have thought about it, and the best place to do this seems like the AppDelegate.

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. 
    // Save data if appropriate. See also applicationDidEnterBackground:.
    /* this is where I want to begin the process of saving application data */
}

I have heard of writing objects (only a select few allowed such as NSString, NSDictionary, NSArray) to file, but can this be kept around between launches of the app, or does the data go away when the user terminates the app?

Question:

Is there a definitive way to write data to the users iPhone that will stick around after the application quits?

Was it helpful?

Solution

The most common approach is to persist whatever data or state you need when your app enters the background. Then the data will be there if the app is killed while in the background and the app is restarted.

How you store the data really depends on what the data is. Writing the data to a file in the app's sandbox is quite common. Small bits of data can also be stored in NSUserDefaults.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top