Save Session Data in iPhone

2020-07-25 07:51发布

问题:

I want to store data at different points in my app, that is accessible to objects across the app. Something similar to session in php or global variables. I know I can use NSUserDefaults but i am not sure how i would keep on adding values to it and then accessing it. Example, first i want to store the Username that is used during login, then on 3rd screen, i want to save the Company of that user that he selects from a list of companies. Then on the 5th screen i want to save the location that the user selects. And then I have different functionalities that the user can use depending on his selections. As I am a newbie i am sorry if this is a dumb question but can anybody help me as to how i would store and retrieve mulitple data that is accessible throughout the app.

回答1:

You asked me in your comment to provide a code snippet on property lists. Sure. No prob. First thing you need to do is create your property list file. Just add a file of type property list to your project. Let's call it DataPoints.plist. For purposes of this example, the file will contain key-value entries of type NSString. In the code example, I'm simply extracting the property list entries and loading up an array. Now you can do whatever you want with the array. Use it to load up a table view or whatever. Hope this helps. If it did, can you please mark it as the accepted answer. Thanks!

NSString *errorDesc;
NSPropertyListFormat format;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"DataPoints" ofType:@"plist"];

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                     propertyListFromData:plistXML
                             mutabilityOption:NSPropertyListMutableContainersAndLeaves
                             format:&format
                             errorDescription:&errorDesc];

if (!temp) {
     // Handle Error
     Log(@"Error reading plist: %@, format: %d", errorDesc, format);
}

NSArray *dataPoints = [NSArray arrayWithArray:[temp objectForKey:@"DataPoints"]];


回答2:

On iOS devices you have various options for storing data. Here are a few you might consider:

  1. Core Data and either a SQL data repository or an XML repository. This is basically Apple's device database framework library available for all apps to use. It is not the easiest of options but it will allow you to store fairly large amounts of diverse data that will be available throughout your app code. In addition, the data is retained between app launches as long as you save it to the persistent data store before app shut down.

  2. Property lists. Property lists are more lightweight than Core Data and you might find them easier to use. They allow storage and retrieval of basic key-value pairs into a persistent property list file. So, you also get the advantage of data retention between app launches as long as you store your data to the property list file before app shut down.

  3. Store data to text files. I'm not sure if this has any advantage over property lists.

  4. User Defaults. I'm not sure that you can so easily add new types of data during app usage. This is used more for providing a collection of default app settings and then allowing the user to personalize them during app usage.

  5. In-memory singleton objects. This could be an option but, of course, once the app shuts down, all data goes away unless it's persisted into permanent data storage somehow.

I'm sure there are other options in addition to these. I will be interested to read about them. I hope this helps.



回答3:

create a singleton object. I know it is not a very nice pattern but it is the easiest solution to your question.