6

In my application, i want to store the date at which the app is started for the first time.
I'm using following code in application:didFinishLaunchingWithOptions method

NSDate* today = [NSDate date];
[[NSUserDefaults standardUserDefaults] registerDefaults: @{@"CONSCIENCE_START_DATE" : today}];

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"CONSCIENCE_START_DATE"]);

But every time when i start the application, its printing the time at which the app is starting. Its not printing the date at which i started the app.

I tried to reset the simulator and ran the code again. Still no success. Can some one point me the error in my code please?

Satyam
  • 15,493
  • 31
  • 131
  • 244
  • 1
    Don't you forget to [defaults synchronize]; ? – Dmitry Zhukov Jul 11 '13 at 15:25
  • 1
    If you set the date every time you launch the app, why do you expect to see anything different than the current date? – rmaddy Jul 11 '13 at 15:26
  • @rmaddy, Sorry, I updated my code – Satyam Jul 11 '13 at 15:27
  • Why do you register a default? What is you goal here? Do you want to store a date only the first time a user ever starts the app? And then you want to be able to read that one-time date? – rmaddy Jul 11 '13 at 15:28
  • @rmaddy, you are right. Only for the first time when the app starts i want to store the date. later i just want to read that one-time date. – Satyam Jul 11 '13 at 15:29

2 Answers2

15

Since your goal to is keep track of the very first time a user starts your app, you need something like this in application:didFinishLaunchingWithOptions:

NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:@"CONSCIENCE_START_DATE"];
if (!date) {
    // This is the 1st run of the app
    date = [NSDate date];
    [[NSUserDefaults standardUserDefaults] setObject:date forKey:@"CONSCIENCE_START_DATE"]; // Save date
}

NSLog(@"First run was %@", date);
Aamir
  • 16,329
  • 10
  • 59
  • 65
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • since registerDefaults will be called first time only, from next start of the app, as the defaults are already set, it wont set the defaults again. NSUserDefaults is expected to store the data permanently. Isn't it? – Satyam Jul 11 '13 at 15:37
  • 3
    No, `registerDefaults` is called every time you call it. `registerDefaults` does NOT write anything. It only provides a value if a call to retrieve a value would otherwise return `nil`. In this case, using `registerDefaults` makes no sense. – rmaddy Jul 11 '13 at 15:42
4

You need to call [[NSUserDefaults standardDefaults] synchronize] after you set the value.

From the Apple Docs:

At runtime, you use an NSUserDefaults object to read the defaults that your application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. The synchronize method, which is automatically invoked at periodic intervals, keeps the in-memory cache in sync with

a user’s defaults database.

Firo
  • 15,448
  • 3
  • 54
  • 74
Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74