【问题标题】:Determine how long iOS App has been closed or has been in the background确定 iOS App 已关闭或已在后台运行多长时间
【发布时间】:2013-11-23 13:33:24
【问题描述】:

正如标题所说:如何确定我的 iOS 应用程序已关闭或在后台运行了多长时间?我需要知道这一点,因为如果应用程序已关闭或已在后台运行超过 3 小时,我想调用一个方法。

【问题讨论】:

  • 把关闭/放到后台的时间记录到一个文件中,然后在启动的时候加载这个文件/放到前台?对我来说听起来并不难。
  • 你为什么要这样做?是因为你想唠叨别人使用你的应用程序吗?如果是这样...请帮我们一个忙,不要发布您的应用程序。
  • 哈哈,不,我不想唠叨别人。

标签: ios


【解决方案1】:

您可以通过在 NSUserDefaults 中保存时间来跟踪应用程序被后台/终止的时间,然后在您的应用程序重新启动后使用它们。试试这个代码(我已经格式化了日期,因为我在我的应用程序中以格式化的方式进一步使用它们。您可以选择忽略日期格式。):

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    NSString *backGroundTime = [dateFormat stringFromDate:[NSDate date]];
    [[NSUserDefaults standardUserDefaults]setValue:backGroundTime forKey:@"backGroundTime"];
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    NSString *foreGroundTime = [dateFormat stringFromDate:[NSDate date]];
    NSString *backGroundTime = [[NSUserDefaults standardUserDefaults]valueForKey:@"backGroundTime"];
    [self minCalculation_backgroundtime:backGroundTime forgroundTime:foreGroundTime];
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

// Call this method to calculate the duration of inactivity
-(void)minCalculation_backgroundtime:(NSString *)backgroundTime forgroundTime:(NSString *)foreGroundTime
{
    NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
    [dateformat setDateFormat:@"MM/dd/yyyy HH:mm:ss"];

    NSDate *lastDate = [dateformat dateFromString:foreGroundTime];
    NSDate *todaysDate = [dateformat dateFromString:backgroundTime];
    NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
    NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
    NSTimeInterval dateDiff = lastDiff - todaysDiff;
    int min = dateDiff/60;
    NSLog(@"Good to see you after %i minutes",min);
}

【讨论】:

  • 为什么在“自 1970 年以来的时间间隔”是一个不需要格式化或解析的单个浮点数时存储格式化的日期/时间?它也更准确。
  • 那是我在我的项目中使用的那块。您可以选择忽略格式。这里的想法是为您提供实现此目标的方法。请随时根据您的方便进行修改。
  • 我敢打赌,您可以通过仅存储时间间隔来减少 80% 的代码。顺便说一句,我不是 OP。
  • 在这种情况下你赢了这个赌注:-)。但我在我的应用上下文中使用了格式化日期。
  • 您甚至可以存储原始的NSDate 对象。无需将其转换为时间间隔。
【解决方案2】:

您可以在NSUSerDefaults 中节省时间以进入后台。当您的应用程序回到前台时,您可以获得那个时间的差异。 当您的应用程序进入后台时,此方法将执行 - (void)applicationDidEnterBackground:(UIApplication *)application,当它返回前台时,- (void)applicationWillEnterForeground:(UIApplication *)application 将调用此方法。

【讨论】:

  • 不要忘记您的应用程序可能在没有调用applicationDidEnterBackground: 的情况下崩溃的情况。用户还可以在不让您进入后台的情况下终止您的应用程序(双击主页,然后滑动以终止)。一般来说,C_X 的方法仍然有效,因为您将查看 last 值(它会更早),但您需要牢记这些情况。如果您使用“我什么时候进入后台”作为其他内容的代理,例如“我上次与服务器通话是什么时候”,我会跟踪您真正关心的事情,而不是您何时进入后台。
  • 还有一个关于何时调用各种方法的有用提示表:stackoverflow.com/questions/3712979/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 2014-10-26
  • 2018-01-12
相关资源
最近更新 更多