【问题标题】:Calculate the seconds while the app was in background计算应用程序处于后台时的秒数
【发布时间】:2013-03-23 22:04:04
【问题描述】:

我试图找出应用在后台运行的时间。一切都可以正常编译,但我的计数器不会随着应用程序在后台运行的时间而增加。

  -(void)applicationWillResignActive:(UIApplication *)application {

    closeTime = [NSDate date];

    NSUserDefaults *defaultsCloseTime;
    [defaultsCloseTime setInteger:closeTime forKey:@"closeTimeKey"];
    [defaultsCloseTime synchronize];

}

-(void)applicationWillEnterForeground:(UIApplication *)application {

    closeTime = [[NSUserDefaults standardUserDefaults] integerForKey:@"closeTimeKey"];

    timeInterval = [closeTime timeIntervalSinceNow] * -100;

}


Counter

在我的视图控制器中:

counterInt = time + counterInt;

【问题讨论】:

  • 添加 NSLog(@"Application Did Resign Active"); 以确保方法被调用。

标签: nsdate nsuserdefaults nstimeinterval


【解决方案1】:

您应该在将关闭时间写入用户默认值后synchronize。同样在您的问题中,您在哪里阅读 counterInt 并且您是否也将其存储到用户默认值?

还有一件事:

NSInterger time = round(timeInterval);

【讨论】:

  • 谢谢!完全错过了同步。这仍然不起作用。我对 timeInterval 没有任何价值
猜你喜欢
  • 2018-03-07
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-24
  • 2017-11-06
  • 1970-01-01
相关资源
最近更新 更多