7

I'm coding an iPad app, and I have to change a graphic when the day changes to Sunday.

My straightforward solution is to check the day in the - (void)applicationDidBecomeActive:(UIApplication *)application or -(void)viewWillAppear:(BOOL)animated methods, and set a timer like every 10 mins to check if the day have changed to Sunday while the app is active.

Is there another, perhaps more efficient, way to handle this?

Moshe
  • 57,511
  • 78
  • 272
  • 425
Ecarrion
  • 4,940
  • 1
  • 33
  • 44

5 Answers5

13

In your appliciation delegate, implement the following method: -(void)applicationSignificantTimeChange:(UIApplication *)application

This method is called when the day changes, or if the device's time has been changed in the background for whatever reason (such as changes to time zone).

zeroimpl
  • 2,746
  • 22
  • 19
4

Another option would be to use the same two methods you mentioned, "viewWillAppear:" and "applicationDidBecomeActive:" and instead of setting a timer for every 10 minutes, just calculate the amount of time between the current time and the next Sunday. Take that time interval and use it to set a timer that will fire exactly at midnight on Sunday.

Carter
  • 4,738
  • 2
  • 22
  • 24
1

There's a notification for when the day changes. You could listen to it and check if it's sunday when it fires.

You should also check if it's sunday at launch.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
1

What's wrong with that solution? Does it become Sunday more frequently than once every 10 minutes in some places?

One thing you might want to consider: in your timer fire method, if the time is after 23:50 then reschedule the timer for sooner (i.e. around 00:00) so you're more likely to catch midnight accurately.

  • 1
    I just don't like the idea of having a timer asking for the date every some amount of time, I Feel kind of dirty ;-) – Ecarrion Nov 15 '11 at 16:51
1

This chapter might prove useful, using some type of Notification to update:

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1

Although I myself ended up with the same solution you are using, it's just easier and more reliable I find.

Alan Moore
  • 6,525
  • 6
  • 55
  • 68
  • Why you think is more reliable? – Ecarrion Nov 15 '11 at 17:03
  • It seems like there are issues when the device goes into deep sleep. I think you might need to register for remote notifications to get this to work correctly... strange that Apple doesn't seem to have a proper solution for this, Android has great support for this sort of thing. – Alan Moore Nov 15 '11 at 17:29