如何检索应用程序在iPhone中关闭的日期和时间?

时间:2011-03-04 06:54:16

标签: iphone objective-c datetime

我正在使用一个应用程序,我需要显示应用程序最后关闭的日期和时间。是否可以存储和检索iphone应用程序打开和关闭的日期和时间?我有一个网络服务,可以存储日期和时间,但我不知道如何检索这个东西。

有什么想法吗?谢谢。

2 个答案:

答案 0 :(得分:2)

使用UIApplicationDelegate的方法:

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

如果您想存储日期,可能需要使用NSUserDefaults。这真的非常简单。检查文档。

以下是另一个帮助链接:What's the optimum way of storing an NSDate in NSUserDefaults?

答案 1 :(得分:0)

你必须在app delegate的功能中执行此操作 - (void)applicationDidEnterBackground:(UIApplication *)application ;

- (void)applicationWillTerminate:(UIApplication *)application ;
获得日期和时间(也是小时和秒)

  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];