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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Macbook Pro freezes when power button is pressed. fun173 MacBook Pro 3 Nov 3, 2012 04:36 AM
iPhone 3G Power Button Always Pressed Down DennizZ iPhone Tips, Help and Troubleshooting 1 Oct 7, 2011 12:28 PM
could the power button be stuck? roxstarrgirl Mac Basics and Help 7 Jul 7, 2011 10:42 PM
iPad 2 Power Button marshy82 iPad 12 Apr 6, 2011 10:01 AM
UITabBarController question: What controller gets the mesages for a button press? SqueegyX iPhone/iPad Programming 2 Sep 30, 2008 01:07 PM


All times are GMT -5. The time now is 09:41 AM.

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

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