我有一个变量,可以跟踪我想在每天开始时重置的用户统计信息。我怎样才能做到这一点?由于不允许应用程序在后台运行,因此似乎每次应用程序处于活动状态时都必须进行检查,但是我不知道如何重置只有一次的变量。这是我想使用的功能:
let beginingOfDay = NSCalendar.currentCalendar().startOfDayForDate(NSDate())
func resetCurrentTime(){
// Date comparision to compare current date and begining of the day.
let dateComparisionResult:NSComparisonResult = NSDate().compare(beginingOfDay)
if dateComparisionResult == NSComparisonResult.OrderedDescending || dateComparisionResult == NSComparisonResult.OrderedSame {
// Current date is greater or equal to end date.
currentTime = 0 //reset the time tracker
}
}
我想使用此功能检查应用程序何时启动,但问题是该应用程序一天可能多次启动。如果用户正在使用该应用程序,或者当该应用程序当天处于活动状态或首次启动时,如何在一天开始时仅重置一次变量?谢谢
您可以在用户默认值中存储此值。
因此,流程如下:
如果应用程序正在运行且日期已更改,则可以通过订阅此通知来更新变量的值: UIApplicationSignificantTimeChangeNotification
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句