What's the protocol for power button being pressed?

Discussion in 'iOS Programming' started by ppn, Feb 2, 2011.

  1. ppn
    macrumors member

    Joined:
    Oct 31, 2010
    #1
    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];
    	}
    	
    }
     
  2. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    That is probably going to be an impossible thing without using private frameworks.
     
  3. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    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.
     
  4. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #4
    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.
     
  5. ppn
    thread starter macrumors member

    Joined:
    Oct 31, 2010
    #5
    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];
    	}
    	
    }
     
  6. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Did you try both 'applicationWillResignActive' and 'applicationDidEnterBackground' as well?
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    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.
     
  8. ppn
    thread starter macrumors member

    Joined:
    Oct 31, 2010
    #8
    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?
     

Share This Page