0

I am working with one application where i need to display the date and time the application was closed lastly.It is possible to store and retrieve the date and time the iphone application was opened and closed? I am having one webservice that would store the date and time but i am not having idea about how to retrieve this thing.

Any idea? Thank you.

Yama
  • 2,649
  • 3
  • 31
  • 63

2 Answers2

2

Use UIApplicationDelegate's methods:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- (void)applicationWillTerminate:(UIApplication *)application

If you want to store date you probably want to use NSUserDefaults. It is really very straightforward. Check docs.

Here is another link to help: What's the optimum way of storing an NSDate in NSUserDefaults?

Community
  • 1
  • 1
Max
  • 16,679
  • 4
  • 44
  • 57
  • 1
    and use [[NSDate date] timeIntervalSince1970] to get the floating point timestamp of that time. – Ankit Mar 04 '11 at 06:58
0

You have to do this in app delegate's function
- (void)applicationDidEnterBackground:(UIApplication *)application ;
or
- (void)applicationWillTerminate:(UIApplication *)application ;
To get date and time (hours and seconds also)

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    NSString *formattime=[dateFormatter stringFromDate:[NSDate date]];
    NSArray *chunks = [formattime componentsSeparatedByString:@":"];
    NSArray *sec = [[chunks objectAtIndex:2] componentsSeparatedByString:@" "];
    //if([[sec objectAtIndex:1] isEqualToString:@"PM"]){
    int hour=[[chunks objectAtIndex:0]  intValue];
    int min= [[chunks objectAtIndex:1]  intValue];
    //pmoram=[sec objectAtIndex:1];
    int second=[[sec objectAtIndex:0] intValue];
    //Use the date and release the dateFormatter
    [dateFormatter release];
SNR
  • 1,249
  • 2
  • 20
  • 38