简体   繁体   中英

getting values in an array after exiting+objective c

In my iphone application I am storing values in an array and I want to retrieve it when I reload the application.That is,When the user exits my app, I want this array to be saved so that when the app is loaded again by the user it want to be accessed.

You can make use of the UIApplicationDelegate methods applicationWillTerminate and applicationDidEnterBackground to save your data. If you created your project from a template they should be implemented with some comments in your appDelegate.

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
// You can save your array here
}

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
    //Save array here
}

To complement previous answers, you can use KeyedArchiver to read / write your object to file.

BOOL result = [NSKeyedArchiver archiveRootObject:your_NSArray toFile:filePath];

and

id archive = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 

Standard iOS directories where your application can store files are accessed through

NSSearchPathForDirectoriesInDomains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM