Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ppn

macrumors member
Original poster
Oct 31, 2010
36
0
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.

Code:
- (void)applicationDidEnterBackground:(UIApplication *)application {
	
	if (self.repeatingTimer != nil) {
		[self stopRepeatingTimer:self];
	}
	
}

- (void)applicationDidEnterForeground:(UIApplication *)application {
	
	if (self.repeatingTimer != nil) {
		[self startRepeatingTimer:self];
	}
	
}
 
That is probably going to be an impossible thing without using private frameworks.
 
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.
 
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.
 
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.

Code:
- (void)applicationWillResignActive:(UIApplication *)application {
	
	if (self.repeatingTimer != nil) {
		[self stopRepeatingTimer:self];
	}
	
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	
	if (self.repeatingTimer != nil) {
		[self startRepeatingTimer:self];
	}
	
}
 
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.
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.