Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

NSTimer to fire when day ends?

Hi,

I am trying to let users complete a task once a day, so when the day ends, the task is enabled again.

Would using NSTimer be the best way to go about this?

1 Answer

I dont think that will work. The app would have to be in use constantly for the timer to work right ( I think). You might try using NSUserDefaults to store the date of the last app open. Then compare that date stored in defaults to the current date. if the current date is past midnight of the stored date, reset the task. something like that is what you are most likely going to have to do

Stone, thanks for your input.

I tried the timer and quickly realized that it was not going to work as you said. After Googling around for a bit, I found UIApplicationSignificantTimeChangeNotification[1] which as Apple says:

"Posted when there is a significant change in time, for example, change to a new day (midnight), carrier time update, and change to or from daylight savings time. This notification does not contain a userInfo dictionary."

I think it was just what I needed.

[1]https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html

hmm. I think you may have the same problem with that solution as well. The app will have to be running for the notification to be received. if the app is not running, the notification wont be received and your reset will not run at all