pav
pav

Reputation: 142

How to store temporary values/objects per session in iOS app

So I've got a hybrid e-commerce iOS app that I'm working on, and I need to store multiple string / array values temporarily for the duration of the checkout process. If the user leaves the checkout process, I want these values either deleted or reset. Otherwise, at periods of time during the checkout process I want to be able to retrieve these values and use them.

What's the best way to achieve this? Core Data seems a little more permanent than I'm looking for, but it might be the right way to go. I can always store stuff in there and delete it when the user leaves the checkout process, but I'm wondering if there's a better way to do it, basically.

A note about the hybrid part:

So, taking that into consideration, where is the best place for me to store the data that is retrieved by the data retrieval class? Sample code, of course, is always welcome :). Thanks!

Upvotes: 1

Views: 1711

Answers (1)

v01d
v01d

Reputation: 559

If the data you're retrieving from WebView is key-value pairs, you need not set up Core data for that. You can simply use NSUserDefaults to store the data. You can save settings and properties related to application or user data using NSUserDefaults. The values will get saved for the entire scope of your application but you can reset the values once you're done reading those values in Checkout process.

Here is how you save data using NSUserDefaults -

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:firstName forKey:@"firstName"];
[defaults synchronize];

You can use the following code to retrieve data -

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstName = [defaults objectForKey:@"firstName"];

Upvotes: 2

Related Questions