PDA

View Full Version : What's the protocol for power button being pressed?




ppn
Feb 2, 2011, 04:07 PM
I'm trying to stop a timer when the power button is pressed but I can't seem to find the right protocol. I've tried applicationDidBecomeActive/applicationWillResignActive and also the EnterBackground/EnterForeground method but my timer only stops if I switch to another application but not when I put it to sleep by pressing the power button. I'm using ios 4.2 on iphone 4. I've looked into the UIApplicationDelegate Protocol Reference and I can't find anything that would check for power button being pressed.

- (void)applicationDidEnterBackground:(UIApplication *)application {

if (self.repeatingTimer != nil) {
[self stopRepeatingTimer:self];
}

}

- (void)applicationDidEnterForeground:(UIApplication *)application {

if (self.repeatingTimer != nil) {
[self startRepeatingTimer:self];
}

}



ulbador
Feb 2, 2011, 04:21 PM
That is probably going to be an impossible thing without using private frameworks.

lloyddean
Feb 2, 2011, 04:32 PM
While I don't know the answer, and therefore probably shouldn't say anything, it seems to me more likely to be one of the 'applicationWill' functions.

ulbador
Feb 2, 2011, 04:40 PM
The applicationWillResignActive is called when the phone goes to sleep either from pushing the top button or the auto lock is engaged. This is probably what you want. I don't think you can detect specifically whether the top button was pressed versus the phone autolocking.

ppn
Feb 2, 2011, 09:54 PM
I tried the applicationWillResignActive method already and it only stops the timer if I start another application but not when I put it to sleep.

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

if (self.repeatingTimer != nil) {
[self stopRepeatingTimer:self];
}

}

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

if (self.repeatingTimer != nil) {
[self startRepeatingTimer:self];
}

}

lloyddean
Feb 2, 2011, 11:23 PM
Did you try both 'applicationWillResignActive' and 'applicationDidEnterBackground' as well?

chown33
Feb 3, 2011, 12:08 AM
I tried the applicationWillResignActive method already and it only stops the timer if I start another application but not when I put it to sleep.

Apply basic debugging practices. Break it down into smaller steps.

1. Confirm that applicationWillResignActive is being called.
2. If it is, then confirm whether the repeatingTimer property is nil or not.
3. If the property isn't nil, then maybe something is going wrong in the stopRepeatingTimer: method, whose code you haven't posted.

I don't see why this can't be decomposed and each step confirmed. It's not an all-or-nothing black box of inaccessible components; these are all individually testable conditions that the debugger can show you.

ppn
Feb 3, 2011, 06:37 PM
Alright, I've isolated my mistake. I was calling the willResignActive in my view controller. I've tried it in the app delegate and now it works but the timer I have is in the view controller (QuizViewController), how can I send a message from my app delegate to my view controller once willResignActive is called?