2

Is there any notification I can listen to that will alert me that the phone will go to sleep? I have implemented the following:

[[NSNotificationCenter defaultCenter] 
              addObserver:self 
                 selector:@selector(resignActive:)
                     name:UIApplicationDidEnterBackgroundNotification
                   object:nil];

but it seems that it's not getting triggered when the phone goes to sleep after some time of inactivity (1 min in my settings). The reason I think it's not working is that I have a timer that perform an action every second and if a certain time has passed (bigger than one minute) it issues an audio alarm. In my resignActive I invalidate my timer and that works fine when I press the home button, but not when the phone goes to sleep. It still seems to be running in the background but "at a lower speed" as the times are much longer than normal (around 10 min instead of 2 min).

Any ideas what's happening when the phone goes to sleep? I have read these two posts but it doesn't really answer my question.

What happens to an iPhone app when iPhone goes into stand-by mode?

Iphone app is delayed for 10 -15 minutes when iphone is in sleep mode

Community
  • 1
  • 1
Structurer
  • 694
  • 1
  • 10
  • 23

1 Answers1

1

Implements those 2 methods on the UIApplicationDelegate :

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

It will be called also when a push notification from another app (or a SMS) is shown...

TheSquad
  • 7,385
  • 8
  • 40
  • 79
  • What a good start of the week. Based on your concise and correct answer I changed my method above to subscribe to UIApplicationWillResignActive. It now works as I want it to. Thanks for a perfect answer. Wish they were all like this. – Structurer Aug 09 '10 at 01:24
  • just happy to help... have a nice week – TheSquad Aug 09 '10 at 09:43