【问题标题】:how to retrieve the date and time the application was closed in iphone?如何检索应用程序在 iphone 中关闭的日期和时间?
【发布时间】:2011-03-04 06:54:16
【问题描述】:

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

有什么想法吗?谢谢。

【问题讨论】:

    标签: iphone objective-c datetime


    【解决方案1】:

    使用 UIApplicationDelegate 的方法:

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

    如果你想存储日期,你可能想使用 NSUserDefaults。这真的很简单。检查文档。

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

    【讨论】:

    • 并使用 [[NSDate date] timeIntervalSince1970] 获取该时间的浮点时间戳。
    【解决方案2】:

    您必须在应用代理的函数中执行此操作
    - (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];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      相关资源
      最近更新 更多