Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 2, 2011, 04:07 PM   #1
ppn
macrumors member
 
Join Date: Oct 2010
What's the protocol for power button being pressed?

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];
	}
	
}
ppn is offline   0 Reply With Quote
Old Feb 2, 2011, 04:21 PM   #2
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 2010
That is probably going to be an impossible thing without using private frameworks.
__________________
Mooooooooooooo
ulbador is offline   0 Reply With Quote
Old Feb 2, 2011, 04:32 PM   #3
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
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.
lloyddean is offline   0 Reply With Quote
Old Feb 2, 2011, 04:40 PM   #4
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 2010
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.
__________________
Mooooooooooooo
ulbador is offline   0 Reply With Quote
Old Feb 2, 2011, 09:54 PM   #5
ppn
Thread Starter
macrumors member
 
Join Date: Oct 2010
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];
	}
	
}
ppn is offline   0 Reply With Quote
Old Feb 2, 2011, 11:23 PM   #6
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Did you try both 'applicationWillResignActive' and 'applicationDidEnterBackground' as well?
lloyddean is offline   0 Reply With Quote
Old Feb 3, 2011, 12:08 AM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by ppn View Post
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.
chown33 is offline   0 Reply With Quote
Old Feb 3, 2011, 06:37 PM   #8
ppn
Thread Starter
macrumors member
 
Join Date: Oct 2010
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?
ppn is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Multiple Images when Button pressed..?? Xcode Phloxx1 iPhone/iPad Programming 2 Feb 22, 2014 04:04 PM
iPhone 5 touchscreen won't respond until lock button is pressed. leshric iPhone Tips, Help and Troubleshooting 0 Sep 16, 2013 04:15 PM
iPhone crashing when apps are put on certain screens and home button is pressed markw10 iPhone Tips, Help and Troubleshooting 0 Nov 15, 2012 06:07 AM
iPad 3rd generation crashing when home button pressed Loke2112 iPad Tips, Help and Troubleshooting 2 Oct 5, 2012 09:00 AM
No sound when button pressed sheepsblood iPhone and iPod touch Apps 0 Aug 26, 2012 01:50 PM

Forum Jump

All times are GMT -5. The time now is 01:47 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC