0

I want to save some initial settings values given by the user when the app is open for the first time, If the values are saved it shouldn't be appear next time. How to save these values inside the app. Some suggested to use .plist , while searched regarding this. Is that the right approach? or there any simpler option available?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dolo
  • 966
  • 14
  • 28

8 Answers8

3

I would suggest saving the information in an array and then saving the array on the NSUserDefaults singleton that is integrated on the device. That way you can always access the information from anywhere.

Have in mind that this approach is only viable if the info is small enough.

To save on the NSUserDefaults class:

[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:@"Key"];

To get the value:

NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] 
                                        objectForKey:@"Key"]];
Jose Luis
  • 3,307
  • 3
  • 36
  • 53
  • Will the values in the NSUserDefaults will be retained even after I closed my app and open again? – Dolo Jun 04 '13 at 09:17
  • 1
    Yes they will be retained, it is a singleton class that is always running, even if the app is closed. This class works for all the applications, remember to give it a unique key so you can get it later. – Jose Luis Jun 04 '13 at 09:20
2

The easiest option is to save these value in form of key-value pair into NSUserDefaults.

 NSUserDefaults *stdDefaults = [NSUserDefaults standardUserDefaults];
 if([stdDefaults objectForKey:@"APP_OPENED"] == FALSE)
 {
      [stdDefaults setValue:@"YOUR_VALUE" forKey:@"YOUR_KEY"];
           //Store more values if you wish
      [stdDefaults setBool:YES forKey:@"APP_OPENED"];
      [stdDefaults synchronize];
 }
Apurv
  • 17,116
  • 8
  • 51
  • 67
2

If you want to save non encrypted data, you can use NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:txtfield1.text forKey:@"info1"];
[defaults setObject:txtfield2.text forKey:@"info2"];
[defaults synchronize];
rdurand
  • 7,342
  • 3
  • 39
  • 72
Jitendra
  • 5,055
  • 2
  • 22
  • 42
1

You can use NSUserDefaults which can store data

Read apple's doc

Robert
  • 19,800
  • 5
  • 55
  • 85
1

Make use of NSUserDefaults to store the data. The stored data can retrieved and modified whenever necessary.

Read NSUserDefaults Class Reference

Read Tutorial

Vinayak Kini
  • 2,919
  • 21
  • 35
1

If it includes passwords, better to use KeyChain. Otherwise, NSUserDefaults would be a good choice...

lakshmen
  • 28,346
  • 66
  • 178
  • 276
1

I would suggest using NSUserDefaults. Something like this:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL hasOpenedAppBefore = [defaults boolForKey:@"hasOpenedAppBefore"];

[defaults setBool:YES forKey:@"hasOpenedAppBefore"];
Mikael
  • 3,572
  • 1
  • 30
  • 43
0

NSUserDefaults is the best option for store data inside application and you can easily use it throughout the application.

kraj
  • 1
  • 1