This is how I do it. Each time your application starts or returns to the forefront, do the following:
Read the saved NSDate from NSUserDefaults.
Determine the number of midnight elapsed from saved to date. (Do a search in Xcode docs in "Midnights" to find the NSCalendar code to calculate the number of days between two dates.)
NSUserDefaults
NSDate:
func dayNumber() -> Int
{
let calendar = DateUtils.gregorianCalendar
return calendar.ordinalityOfUnit(NSCalendarUnit.CalendarUnitDay,
inUnit: NSCalendarUnit.CalendarUnitEra, forDate: self)
}
DateUtils NSCalendar. , .
class DateUtils
{
static let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
}