views:

326

answers:

4

Hello Experts!

I have created an app that uses NSTimer, which gets triggered each second.

My problem is that if the Iphone is in sleep mode i get a delay for 10 to 15 minutes before the event is triggered. I have stackoverflowed and googled this and the reason for this seems to be that the phone stops listening for certain events when in sleep mode.

Some people have solved this issue by playing a mute sound, not allowing the phone to sleep.

  1. What could be the reason for the delay?
  2. The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
  3. If I use the mute sound solution will it the pass the apple review?

    Code:

    timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(goAction)userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    

    -(void)goAction { Here i check for some dates and then call the activateBeepAlarmView }

A: 

Hi Jakob,

What happens in your app when the NSTimer is triggered each second? Please provide code showing the creation of the timer as well as the code for the selector that is called when the timer completes.

Also what do you mean by a "delay for 10 to 15 minutes"? Is the delay always that long or is that how long you wait to awaken the iPhone and then the event is triggered?

Depending on what you need to do every second you can handle this situation in different ways. Please respond and we'll try to work our way through this.

Bart

Bart Gottschalk
By delay I mean that the activateBeepAlarmView is called and the phone starts to beep without me pressing the powerbutton to wake it up from sleep/lock mode. If i press the powerbutton then the app 'wakes' up and plays the beep.
jakob
+1  A: 

When the iPhone goes to sleep, so does your app and the runloop that runs the NSTimer.

You seem to think that an NSTimer is some sort of hardware based timer. It is not. It operates completely within the software of the app that launches it. I don't know what is waking your app up but it is definitely not the NSTimer.

In short, what you want to do is impossible. You can't sleep the phone and then have an app still active and running. The mute sound technique is just a kludge to keep the phone awake and the app running.

If you need the phone to stay awake, you need to set the application's idleTimerDisabled to YES. This will prevent the phone from sleeping and the app can remain active. But once you let the phone sleep, it cannot be awaken from app code. Only the hardware can do that in response to an alarm or an incoming message.

TechZen
Does this mean that every app that works when the user has used the power button for lock mode, must use some sort of hack for it to not go to sleep? E.g Alaram Clock Pro?
jakob
As far as I know. I don't think Alarm Clock Pro works either if you shut off the device. Certainly, they don't advertise that it does. A basic rule of iPhone design is that non-Apple apps cannot override hardware. Think of all the trouble apps could get up to if they could wake the device up when no one was looking or worse shut it down. You might be able to do something with the low level BSD routines but I seriously doubt it.
TechZen
Hi again, thank you for your answer but i don't think we really understand each other. I'm not talking about shutting of the device but to lock the screen with the power button, which will result in the iphone going to sleep mode(correct me if i'm wrong).
jakob
As far as I know, hitting the power button shuts down all apps and pretty much all of the OS. The waking for a phone call, alarm etc is done by hardware and I don't think the API allows access to those functions. I haven't seen anything that suggest that an app can wake the device once the user has used the hardware switch to turn it off. Even if I am wrong about all that, I can tell you definitively that NSTimer will not run when the device is asleep. NSTimer is purely software and is hardware agnostic. You would be looking for something much lower level. Look at IORegister.
TechZen
Thank you for the tips! But I'm still a little bit confound about your description of the power button. It sounds like you are talking about the home button?? But still, how come the app 'wakes upp' after 10 to 15 minutes, could it be that the iphone when it goes to 'deep sleep mode'(don't know whats it called) the phone still checks if something happens but in a 10 - 15 min interval.
jakob
+2  A: 

Well since no one has answered my three questions I will have to answer them:

1. What could be the reason for the delay? I will have to quote Ben S:

Once applicationWillResignActive gets called on your application you simply stop receiving events: The delegate can implement this method to make adjustments when the application transitions from an active state to an inactive state. When an application is inactive, it is executing but is not dispatching incoming events. This occurs when an overlay window pops up or when the device is locked.

The point of sleep mode is to save energy. To do so, the device stops listening for events like the ones you're asking for. NSTimer events will still fire since they don't require expensive (battery-wise) hardware monitoring. Also, alarms are implemented using NSTimer, so they need to be able to function even when in sleep. Otherwise, people might not wake up and blame their iPhone.

2. The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
No, currently I haven't found another solution, please feel free to correct me if I'm wrong. Check out this blog post how to do it.

3. If I use the mute sound solution will it the pass the apple review? Yes

jakob
A: 

@Jakob,

This is impossible with the "Official SDK". If you're developing apps for jail broken phone, then you can use IOKit framework for this. For more info please refer this.

prathumca