As I have promised three weeks ago (and again a few days ago) I finally got around to writing this little how-to. I’m sorry for the delay. I hope this helps someone, anyway.
Mind you, this is my first attempt at writing a how-to and most of it was done while I was being tired. Also, English isn’t my native language, so please excuse any typos or other errors you may find.
If you have any suggestions on how to improve this how-to, please feel free to leave a comment.
Now, have fun!
Preface
We will create an iPhone application, that prevents the iPhone from deep sleeping while the app is running. It’s not a really useful application, as it does nothing but just that, but you can easily adapt the stuff you learn here and use it for your own application(s).
Here’s what we’re going to do: We will …
- … create a new project in Xcode.
- … add the necessary frameworks to this project.
- … add a silent sound file to this project.
- … create a class called “DeepSleepPreventer” and add it to this project.
- … then use this class to prevent the iPhone from deep sleeping.
You can use this class in your apps to prevent the iPhone from deep sleeping, while they are running. Just add the needed frameworks and the DeepSleepPreventer class to your app and use the DeepSleepPreventer, whenever you need it.
Create a New Project in Xcode
I’m sure you all know how to do this, but for completeness’ sake, I’ll just go through the whole process, step by step.
Fire up Xcode. Click “File” -> “New Project”. We are gonna use the “View-based Application” template for this how-to, so select it and click “Choose …”.

Choose a location and name under which you want to save the project and click on “Save”. We are going to use “iPhoneInsomnia” as a name and save the project to the desktop (I know, I have a lot to tidy, judging by the screenshot).

Add the Necessary Frameworks
Now we’ll add the AVFoundation and the AudioToolbox frameworks to our project, as we will need them later on.
In our project window, we right-click (or control-click) our “Frameworks” group in the “Groups & Files” pane and click on “Add” -> “Existing Fameworks …”.

A file selection window should pop up now and should show you the content of the current iPhone SDK’s “Library” subfolder. If your starting up somewhere else, navigate to “/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/System/Library” first (see screenshot).

Further go to the Frameworks folder and select “AudioToolbox.framework” and “AVFoundation.framework”. Then click on “Add”.

In the upcoming window uncheck the “Copy …” checkbox, if it is selected and choose “Relative to Current SDK” as reference type. Then click “Add”.

Add a Silent Sound File
Now we need to add a silent sound file. You can create it with a sound tool of your choice. I named it “noSound.wav”. You can just use the file I created, if you like. It is part of the Github repository I created for this whole project. iPhoneInsomnia @ Github
We will first create a new group, named “DeepSleepPreventer” in the “Groups & Files” pane. Because we are very tidy people, we will add the sound file and our later created custom class to this group, too.
Right-click on “iPhoneInsomnia” and click on “Add” -> “New Group” and name it “DeepSleepPreventer”.

Now right-click this group and click “Add” -> “Existing Files …” and add the sound file to the project.

Navigate to the location of your sound file, select it and click “Add”.

Check the “Copy items …” checkbox and set “Reference Type” to “Default”. Click Add.

Create our New Class
Now it’s time to finally write some code. Almost. First we need to add a new class to the project.
Right-click the “DeepSleepPreventer” group and click “Add” -> “New File …”.

Choose the “Objective-C class” template under “iPhone OS” -> “Cocoa Touch Class” and select “NSObject” on the “Subclass of” pull down. Click “Next”.

Name the file “DeepSleepPreventer.m”, Check the “Also create .h” checkbox and click “Finish”.

Now modify “DeepSleepPreventer.h” to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
We declared two properties here: an AVAudioPlayer, that will play our silent sound file and an NSTimer that will call -playPreventSleepSound. We actually create this timer in -startPreventSleep and invalidate it in -stopPreventSleep later.
Now we edit “DeepSleepPreventer.m” step by step.
First we import some header files, synthesize our accessors and mutators (or getters and setters, if you prefer to call them that):
1 2 3 4 5 6 7 8 | |
Modify -init to set up all the stuff we need to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
Now we implement -playPreventSleepSound:
1 2 3 | |
This should be self explanatory.
Now -startPreventSleep (this is the method we will call, if we want the iPhone to stay awake):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
-stopPreventSleep stops the sleep preventing.
1 2 3 4 | |
Finally -dealloc to clean up our mess:
1 2 3 4 5 6 7 8 | |
Use the DeepSleepPreventer Class
Now switch to “iPhoneInsomniaAppDelegate.h”. We will add a property if the type or our class DeepSleepPreventer to the application delegate now. Modify “iPhoneInsomniaAppDelegate.h”, to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Now move on to “iPhoneInsomniaAppDelegate.m”. We will instantiate and use our DeepSleepPreventer in -applicationDidFinishLaunching:. In your own app, you would do this in a place, where it fits best.
First we need to import our class header file and synthesize our new property:
1 2 3 4 5 6 7 8 9 | |
Now modify -applicationDidFinishLaunching: to look like this:
1 2 3 4 5 6 7 8 | |
And finally the rest of “iPhoneInsomniaAppDelegate.m”. Release our property in -dealloc:
1 2 3 4 5 6 7 8 9 | |
Well, that’s it! We are done! :)
Use This in Your Own App
Just add the two frameworks “AVFoundation.framework” and “AudioToolbox.framework” to your own app, add the sound file “noSound.wav” and the files “DeepSleepPreventer.h” and “DeepSleepPreventer.m” to it and instantiate and use an object of the “DeepSleepPreventer” class where it fits best, and you’re ready to go.
License
The source code of this how-to is licensed under the New BSD License. Here comes the license text:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
Issues and Epilog
I couldn’t start my MPMusicPlayer in my music alarm clock app, while I was playing the silent sound with the AVAudioPlayer. So I’m guessing, it’s not possible to play other sounds while this sound plays, at least not via the MPMediaPlayer class.
As I didn’t use any other sounds in my applications yet, I don’t know if this assumption is correct. If you have any more experience with this or any more other useful or relevant information, please leave a comment. I will then update this how-to.
Also, as Shabbir Vijapura pointed out in a comment to my previous post there might be an easier way, to run timers on the iPhone, while it is asleep. I haven’t tested this myself, yet, though, so I cannot guarantee, that this really works. I will test it in the the next couple of days and then update this post, if I find the time.
Update: Apparently this method, doesn’t work, as Shabbir pointed out in the comments.
Furthermore, if you have any experience yourself, dealing with the iPhone’s deep sleep behaviour and would like to share, or if you have any other suggestions or questions, please feel free to leave a comment.
Thanks for reading. I hope this is useful to someone out there.
Downloads
You can download a drop-in piece of code and the silent sound file from GitHub: MMPDeepSleepPreventer on Github
Or you can download the whole demo project, including all source code and the silent sound file: iPhoneInsomnia on GitHub
All the code is licensed under the New BSD License. Feel free, to use it in your own apps. Some attribution would be appreciated.