views:

704

answers:

1

I want my app to keep running and interacting with the user, even if the user presses the sleep button.
So far I have learned, that my app can stay alive with the following code:

NSTimer *timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

This succeeds. The method
- (void)timerFireMethod:(NSTimer*)theTimer
will be called every 0.5s even if I am in sleep mode.

So know my problem is: How can I interact with my Application in sleep mode? I simply need a trigger to start playing a custom audio file. Therefore I need a trigger event, something like detecting:
-a touch at the display
-an acceleration activity
-a MotionShake Event

But after getting the applicationWillResignActive-Event, nothing of the above is working. My accelerometer stops delivery of acceleration events. And I am unable to start them again. MotionShake Event will not be delivered and I don't know, if it is possible to catch a touch event while sleeping.

Update
I expect the user to use my app for lets say 5hours. If it is idle for some time, it will go to sleep on its own. I probably misinterpreted what I mean with sleep mode. The Iphone should be up and running all the time, but I do not need the display and the background light to be on all the time (these are the most power consuming parts). I know that while playing a noiseless sound every 10 seconds, the iphone is not going to sleep. And I know that the IPod application is able to run in that mode and the user can interact with it by shaking the device. So isn't there any official way for an app to do the same?
---

Has anyone any idea?

Thanks Zensursula

+1  A: 

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.

Ben S
Thanks for the explanation for what is going on. I updated my question to explain in more detail for what I want. May be there is another soultion for my problem.
Zensursula