NSTimer doesn't work in sleep mode? - iphone

In my App, i have set the timer for 3, 10 and 15 mins,but it goes to sleep mode and the audio doesn't ring.And after a long time it starts vibrating.Please help me with this issue.
Thanks in advance.

To save battery and resources, not all apps can run in the background, so when the device goes to sleep, your app might not be killed, but it does stop executing.
You can use UILocalNotifications to play the sound you want even if your app is not running.

Timer does not work in deep sleep mode, but you can make app awake by applying idleTimerDisabled property to TRUE, but remember, if you use this, battery will be utilized more....

Related

Allow iphone to sleep while updating ui

I am writing a music player that I am trying to allow to go to sleep mode. The problem is, every second, the ui is updated and I think that is preventing the phone from sleep. I added this line:
[UIApplication sharedApplication].idleTimerDisabled = NO;
and it did nothing. I have seen other apps that go to sleep mode even if the ui is being updated. How can I accomplish this?
Based on the comments, Are you running your app while attached to Xcode? By default Xcode stops the device going to sleep. If so, just disconnect and run, it should go to sleep (provided your devices sleep timer is not too much)

iOS 5 deep sleep prevention

I'm trying to build an alarm app that can fire an alarm while in locked-screen mode (the app is in the foreground, but the screen is locked). The alarm has to be triggered by a NSTimer not by uilocalnotification.
In iOS 4 I used the 'play silent sound every 10 seconds' hack to prevent the app from going to deep sleep and the timer events worked fine. However, in iOS 5 this doesn't seem to work.
Any ideas? Or this should work and I'm doing something wrong?
It seems that you actually can use the 'play silent audio' hack in iOS 5, but the audio has to be audible meaning you can't play it at volume set to 0.0.
You can use github.com/marcop/iPhoneInsomnia and set the volume to greater than 0, but it still doesn't work because the sound file is so short that the system kills your application before the timer is triggered and replays the sound. I solved this by setting the numberOfLoops of the audioPlayer to -1 (infinite repeat). Then it should work.
And you should also set the UIBackgroundMode plist key to an array of one string called "audio"
It's probably a dirty workaround, but in the past I have used the proximity sensor to turn off the screen instead of locking the phone. Simply place the phone upside down and the screen will turn itself off.
[[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
This will allow you to retain full control over the device, while the screen does turn off.

App Background issue While in Background

My app is checking some algorithm in the background, after something like 30 minutes
my application seems to be like close (iphone turn to sleep mode i think). I need the app
to run this algorithm as long as it is in the background.
Do you have any idea how to set this 20 minutes timer to recount by some action?
maybe NSRunloop or something else?
You can't reset this timer or continue to run in the background on a stock OS iPhone (except for a limited amount of time, or if your is legitimately of 3 types, VoIP, audio or GPS).
The user can reset this timer by bringing your app to the foreground, perhaps after a notification or something.

Iphone app is delayed for 10 -15 minutes when iphone is in sleep mode

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.
What could be the reason for the delay?
The mute sound solution seems to be a very "dirty" one. Is there some other way to solve this?
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
}
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
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.
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
#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.

Running IPhone apps while in sleep mode

As of IPhone OS 3.0, MotionX GPS is able to keep recording a GPS track while in sleep mode. After you push the sleep button on top of the phone, the app continues receiving GPS coordinates and recording them. Does anyone know how they're able to do this? I'm only aware of the method to keep running audio while in sleep mode.
I found a great webpage that explains how to do it -
Cached link from Wayback machine
Original Link, now 404
You have to start a timer that plays a sound every 10 seconds. But the sound can be silent.
[UIApplication sharedApplication].idleTimerDisabled=YES should do it
I heard that the [UIApplication sharedApplication].idleTimerDisabled=YES will leave the screen's background light on, (after the user pressed the sleep button,) which will drain the battery.
But I have not tried this myself, so anybody with experiences correct this if it is wrong.