0

I want to display the date, the app I make was opened for the first time in view (which is the only view in the whole app for now).

What kind of object do I use best in my view to display the date and how do I get it in there?

Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46
herbigt
  • 55
  • 1
  • 1
  • 6
  • Do you want this date value to always be displayed while the app is running? How does the app work? Does it have more than one screen/viewcontroller? – bneely Sep 28 '13 at 20:54
  • yes, I want the value to be always displayed while its running since it should remind the user when he started to use it. For 1.0 the app won't have more than one screen. – herbigt Sep 28 '13 at 20:56

2 Answers2

2

In your AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    if(![[NSUserDefaults standardUserDefaults] objectForKey:@"firstOpenDate"])
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
        [[NSUserDefaults standardUserDefaults] setObject:[dateFormatter stringFromDate:[NSDate date]] forKey:@"firstOpenDate"];
    }
    NSLog(@"First Opened: %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"firstOpenDate"]);


    return YES;
}

Simply change the setDateStyle:NSDateFormatterLongStyle if you want other formats. You can query NSUserDefaults from anywhere.

In ViewController.m

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hi. You first opened the app on" message:[[NSUserDefaults standardUserDefaults] objectForKey:@"firstOpenDate"] delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil];
    [alert show];
}
mmackh
  • 3,550
  • 3
  • 35
  • 51
  • ok, thanks. And how do I display this value in my viewcontroller? – herbigt Sep 28 '13 at 21:02
  • Ok, that's quite cool :)But I'd like to show the date directly under a Label with the text "days without xyz since" I already created on my viewcontroller. How could I get that? – herbigt Sep 28 '13 at 21:14
  • This is completely different than what you've asked for. I won't write any code but the trick is storing the [NSDate date] object directly in NSUserDefaults and then comparing it to today's date: http://stackoverflow.com/questions/4739483/number-of-days-between-two-nsdates – mmackh Sep 28 '13 at 21:17
0

On first launch, check to see if you've saved a date to user defaults using objectForKey. If not, save today's date

[NSDate date]

To user defaults using setObject:forKey:

Then fall into code that reads the saved date and compares to see how many days have past between that date and today. Take a look at the NSCalendar method components:fromDate:toDate:options

You might want to do a search on "Performing Calendar Calculations" in Xcode and read the resulting chapter.

Duncan C
  • 128,072
  • 22
  • 173
  • 272