<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[blog.marcopeluso.com]]></title>
  <link href="http://blog.marcopeluso.com/feed/atom.xml" rel="self"/>
  <link href="http://blog.marcopeluso.com/"/>
  <updated>2013-03-27T17:17:04+01:00</updated>
  <id>http://blog.marcopeluso.com/</id>
  <author>
    <name><![CDATA[Marco Peluso]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[How-to: Prevent iPhone from Deep Sleeping]]></title>
    <link href="http://blog.marcopeluso.com/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/"/>
    <updated>2009-08-23T13:09:00+02:00</updated>
    <id>http://blog.marcopeluso.com/2009/08/23/how-to-prevent-iphone-from-deep-sleeping</id>
    <content type="html"><![CDATA[<p>As I have promised three weeks ago (and again a few days ago) I finally got around to writing this little how-to.
I&#8217;m sorry for the delay. I hope this helps someone, anyway.</p>

<p>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&#8217;t my native language, so please excuse any typos or other errors you may find.</p>

<p>If you have any suggestions on how to improve this how-to, please feel free to leave a comment.</p>

<p>Now, have fun!</p>

<h2>Preface</h2>

<p>We will create an iPhone application, that prevents the iPhone from deep sleeping while the app is running. It&#8217;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).</p>

<p>Here&#8217;s what we&#8217;re going to do:
We will &#8230;</p>

<ul>
<li>&#8230; create a new project in Xcode.</li>
<li>&#8230; add the necessary frameworks to this project.</li>
<li>&#8230; add a silent sound file to this project.</li>
<li>&#8230; create a class called &#8220;DeepSleepPreventer&#8221; and add it to this project.</li>
<li>&#8230; then use this class to prevent the iPhone from deep sleeping.</li>
</ul>


<p>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.</p>

<h2>Create a New Project in Xcode</h2>

<p>I&#8217;m sure you all know how to do this, but for completeness&#8217; sake, I&#8217;ll just go through the whole process, step by step.</p>

<p>Fire up Xcode. Click &#8220;File&#8221; -> &#8220;New Project&#8221;. We are gonna use the &#8220;View-based Application&#8221; template for this how-to, so select it and click &#8220;Choose &#8230;&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-11.png" title="how-to-1-1" alt="how-to-1-1" /></p>

<p>Choose a location and name under which you want to save the project and click on &#8220;Save&#8221;. We are going to use &#8220;iPhoneInsomnia&#8221; as a name and save the project to the desktop (I know, I have a lot to tidy, judging by the screenshot).</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-2.png" title="how-to-1-2" alt="how-to-1-2" /></p>

<h2>Add the Necessary Frameworks</h2>

<p>Now we&#8217;ll add the AVFoundation and the AudioToolbox frameworks to our project, as we will need them later on.</p>

<p>In our project window, we right-click (or control-click) our &#8220;Frameworks&#8221; group in the &#8220;Groups &amp; Files&#8221; pane and click on &#8220;Add&#8221; -> &#8220;Existing Fameworks &#8230;&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-3.png" title="how-to-1-3" alt="how-to-1-3" /></p>

<p>A file selection window should pop up now and should show you the content of the current iPhone SDK&#8217;s &#8220;Library&#8221; subfolder. If your starting up somewhere else, navigate to &#8220;/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/System/Library&#8221; first (see screenshot).</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-4.png" title="how-to-1-4" alt="how-to-1-4" /></p>

<p>Further go to the Frameworks folder and select &#8220;AudioToolbox.framework&#8221; and &#8220;AVFoundation.framework&#8221;. Then click on &#8220;Add&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-5.png" title="how-to-1-5" alt="how-to-1-5" /></p>

<p>In the upcoming window uncheck the &#8220;Copy &#8230;&#8221; checkbox, if it is selected and choose &#8220;Relative to Current SDK&#8221; as reference type. Then click &#8220;Add&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-6.png" title="how-to-1-6" alt="how-to-1-6" /></p>

<h2>Add a Silent Sound File</h2>

<p>Now we need to add a silent sound file. You can create it with a sound tool of your choice. I named it &#8220;noSound.wav&#8221;. You can just use the file I created, if you like. It is part of the Github repository I created for this whole project. <a href="http://github.com/marcop/iPhoneInsomnia/tree/master">iPhoneInsomnia @ Github</a></p>

<p>We will first create a new group, named &#8220;DeepSleepPreventer&#8221; in the &#8220;Groups &amp; Files&#8221; pane. Because we are very tidy people, we will add the sound file and our later created custom class to this group, too.</p>

<p>Right-click on &#8220;iPhoneInsomnia&#8221; and click on &#8220;Add&#8221; -> &#8220;New Group&#8221; and name it &#8220;DeepSleepPreventer&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-10.png" title="how-to-1-10" alt="how-to-1-10" /></p>

<p>Now right-click this group and click &#8220;Add&#8221; -> &#8220;Existing Files &#8230;&#8221; and add the sound file to the project.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-13.png" title="how-to-1-13" alt="how-to-1-13" /></p>

<p>Navigate to the location of your sound file, select it and click &#8220;Add&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-14.png" title="how-to-1-14" alt="how-to-1-14" /></p>

<p>Check the &#8220;Copy items &#8230;&#8221; checkbox and set &#8220;Reference Type&#8221; to &#8220;Default&#8221;. Click Add.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-15.png" title="how-to-1-15" alt="how-to-1-15" /></p>

<h2>Create our New Class</h2>

<p>Now it&#8217;s time to finally write some code. Almost. First we need to add a new class to the project.</p>

<p>Right-click the &#8220;DeepSleepPreventer&#8221; group and click &#8220;Add&#8221; -> &#8220;New File &#8230;&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-16.png" title="how-to-1-16" alt="how-to-1-16" /></p>

<p>Choose the &#8220;Objective-C class&#8221; template under &#8220;iPhone OS&#8221; -> &#8220;Cocoa Touch Class&#8221; and select &#8220;NSObject&#8221; on the &#8220;Subclass of&#8221; pull down. Click &#8220;Next&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-17.png" title="how-to-1-17" alt="how-to-1-17" /></p>

<p>Name the file &#8220;DeepSleepPreventer.m&#8221;, Check the &#8220;Also create .h&#8221; checkbox and click &#8220;Finish&#8221;.</p>

<p><img src="http://blog.marcopeluso.com/images/posts/2009/08/23/how-to-prevent-iphone-from-deep-sleeping/how-to-1-18.png" title="how-to-1-18" alt="how-to-1-18" /></p>

<p>Now modify &#8220;DeepSleepPreventer.h&#8221; to look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="cp">#import &lt;Foundation/Foundation.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="k">@class</span> <span class="nc">AVAudioPlayer</span>;
</span><span class='line'>
</span><span class='line'><span class="k">@interface</span> <span class="nc">DeepSleepPreventer</span> : <span class="nc">NSObject</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">AVAudioPlayer</span> <span class="o">*</span><span class="n">audioPlayer</span><span class="p">;</span>
</span><span class='line'>    <span class="n">NSTimer</span> <span class="o">*</span><span class="n">preventSleepTimer</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">retain</span><span class="p">)</span> <span class="n">AVAudioPlayer</span> <span class="o">*</span><span class="n">audioPlayer</span><span class="p">;</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">retain</span><span class="p">)</span> <span class="n">NSTimer</span> <span class="o">*</span><span class="n">preventSleepTimer</span><span class="p">;</span>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">playPreventSleepSound</span><span class="p">;</span>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">startPreventSleep</span><span class="p">;</span>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">stopPreventSleep</span><span class="p">;</span>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>We declared two properties here: an <code>AVAudioPlayer</code>, that will play our silent sound file and an <code>NSTimer</code> that will call <code>-playPreventSleepSound</code>. We actually create this timer in <code>-startPreventSleep</code> and invalidate it in <code>-stopPreventSleep</code> later.</p>

<p>Now we edit &#8220;DeepSleepPreventer.m&#8221; step by step.</p>

<p>First we import some header files, synthesize our accessors and mutators (or getters and setters, if you prefer to call them that):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="cp">#import &quot;DeepSleepPreventer.h&quot;</span>
</span><span class='line'><span class="cp">#import &lt;AVFoundation/AVFoundation.h&gt;</span>
</span><span class='line'><span class="cp">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="k">@implementation</span> <span class="nc">DeepSleepPreventer</span>
</span><span class='line'>
</span><span class='line'><span class="k">@synthesize</span> <span class="n">audioPlayer</span><span class="p">;</span>
</span><span class='line'><span class="k">@synthesize</span> <span class="n">preventSleepTimer</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Modify <code>-init</code> to set up all the stuff we need to:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">id</span><span class="p">)</span><span class="nf">init</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">((</span><span class="n">self</span> <span class="o">=</span> <span class="p">[</span><span class="n">super</span> <span class="n">init</span><span class="p">]))</span> <span class="p">{</span>
</span><span class='line'>        <span class="c1">// Activate audio session</span>
</span><span class='line'>        <span class="n">AudioSessionSetActive</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
</span><span class='line'>        <span class="c1">// Set up audio session, to prevent iPhone from deep sleeping, while playing sounds</span>
</span><span class='line'>        <span class="n">UInt32</span> <span class="n">sessionCategory</span> <span class="o">=</span> <span class="n">kAudioSessionCategory_MediaPlayback</span><span class="p">;</span>
</span><span class='line'>        <span class="n">AudioSessionSetProperty</span> <span class="p">(</span>
</span><span class='line'>            <span class="n">kAudioSessionProperty_AudioCategory</span><span class="p">,</span>
</span><span class='line'>            <span class="k">sizeof</span> <span class="p">(</span><span class="n">sessionCategory</span><span class="p">),</span>
</span><span class='line'>            <span class="o">&amp;</span><span class="err">#</span><span class="mo">03</span><span class="mi">8</span><span class="p">;</span><span class="n">sessionCategory</span>
</span><span class='line'>        <span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// Set up sound file</span>
</span><span class='line'>        <span class="n">NSString</span> <span class="o">*</span><span class="n">soundFilePath</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSBundle</span> <span class="n">mainBundle</span><span class="p">]</span> <span class="nl">pathForResource:</span><span class="s">@&quot;noSound&quot;</span>
</span><span class='line'>                                                                  <span class="nl">ofType:</span><span class="s">@&quot;wav&quot;</span><span class="p">];</span>
</span><span class='line'>        <span class="n">NSURL</span> <span class="o">*</span><span class="n">fileURL</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSURL</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initFileURLWithPath:</span><span class="n">soundFilePath</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// Set up audio player with sound file</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">audioPlayer</span> <span class="o">=</span> <span class="p">[[</span><span class="n">AVAudioPlayer</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithContentsOfURL:</span><span class="n">fileURL</span> <span class="nl">error:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>        <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">audioPlayer</span> <span class="n">prepareToPlay</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// You may want to set this to 0.0 even if your sound file is silent.</span>
</span><span class='line'>        <span class="c1">// I don&#39;t know exactly, if this affects battery life, but it can&#39;t hurt.</span>
</span><span class='line'>        <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">audioPlayer</span> <span class="nl">setVolume:</span><span class="mf">0.0</span><span class="p">];</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">self</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we implement <code>-playPreventSleepSound</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">playPreventSleepSound</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">audioPlayer</span> <span class="n">play</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This should be self explanatory.</p>

<p>Now <code>-startPreventSleep</code> (this is the method we will call, if we want the iPhone to stay awake):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">startPreventSleep</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// We need to play a sound at least every 10 seconds to keep the iPhone awake.</span>
</span><span class='line'>    <span class="c1">// We create a new repeating timer, that begins firing now and then every ten seconds.</span>
</span><span class='line'>    <span class="c1">// Every time it fires, it calls -playPreventSleepSound</span>
</span><span class='line'>    <span class="n">self</span><span class="p">.</span><span class="n">preventSleepTimer</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSTimer</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithFireDate:</span><span class="p">[</span><span class="n">NSDate</span> <span class="nl">dateWithTimeIntervalSinceNow:</span><span class="mi">0</span><span class="p">]</span>
</span><span class='line'>                                                      <span class="nl">interval:</span><span class="mf">10.0</span>
</span><span class='line'>                                                        <span class="nl">target:</span><span class="n">self</span>
</span><span class='line'>                                                      <span class="nl">selector:</span><span class="k">@selector</span><span class="p">(</span><span class="n">playPreventSleepSound</span><span class="p">)</span>
</span><span class='line'>                                                      <span class="nl">userInfo:</span><span class="nb">nil</span>
</span><span class='line'>                                                       <span class="nl">repeats:</span><span class="n">YES</span><span class="p">];</span>
</span><span class='line'>    <span class="c1">// We add this timer to the current run loop</span>
</span><span class='line'>    <span class="n">NSRunLoop</span> <span class="o">*</span><span class="n">runLoop</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSRunLoop</span> <span class="n">currentRunLoop</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">runLoop</span> <span class="nl">addTimer:</span><span class="n">self</span><span class="p">.</span><span class="n">preventSleepTimer</span> <span class="nl">forMode:</span><span class="n">NSDefaultRunLoopMode</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>-stopPreventSleep</code> stops the sleep preventing.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">stopPreventSleep</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">preventSleepTimer</span> <span class="n">invalidate</span><span class="p">];</span>
</span><span class='line'>    <span class="n">self</span><span class="p">.</span><span class="n">preventSleepTimer</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Finally <code>-dealloc</code> to clean up our mess:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">dealloc</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// memory management</span>
</span><span class='line'>    <span class="p">[</span><span class="n">preventSleepTimer</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">audioPlayer</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">super</span> <span class="n">dealloc</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="c1">// Don&#39;t forget the matching @end to our @implementation</span>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Use the DeepSleepPreventer Class</h2>

<p>Now switch to &#8220;iPhoneInsomniaAppDelegate.h&#8221;. We will add a property if the type or our class <code>DeepSleepPreventer</code> to the application delegate now. Modify &#8220;iPhoneInsomniaAppDelegate.h&#8221;, to look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="cp">#import &lt;UIKit/UIKit.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="k">@class</span> <span class="nc">iPhoneInsomniaViewController</span>;
</span><span class='line'><span class="k">@class</span> <span class="nc">DeepSleepPreventer</span>;
</span><span class='line'>
</span><span class='line'><span class="k">@interface</span> <span class="nc">iPhoneInsomniaAppDelegate</span> : <span class="nc">NSObject</span>  <span class="p">{</span>
</span><span class='line'>    <span class="n">UIWindow</span> <span class="o">*</span><span class="n">window</span><span class="p">;</span>
</span><span class='line'>    <span class="n">iPhoneInsomniaViewController</span> <span class="o">*</span><span class="n">viewController</span><span class="p">;</span>
</span><span class='line'>    <span class="n">DeepSleepPreventer</span> <span class="o">*</span><span class="n">deepSleepPreventer</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">retain</span><span class="p">)</span> <span class="kt">IBOutlet</span> <span class="n">UIWindow</span> <span class="o">*</span><span class="n">window</span><span class="p">;</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">retain</span><span class="p">)</span> <span class="kt">IBOutlet</span> <span class="n">iPhoneInsomniaViewController</span> <span class="o">*</span><span class="n">viewController</span><span class="p">;</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">retain</span><span class="p">)</span> <span class="n">DeepSleepPreventer</span> <span class="o">*</span><span class="n">deepSleepPreventer</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now move on to &#8220;iPhoneInsomniaAppDelegate.m&#8221;. We will instantiate and use our <code>DeepSleepPreventer</code> in <code>-applicationDidFinishLaunching</code>:. In your own app, you would do this in a place, where it fits best.</p>

<p>First we need to import our class header file and synthesize our new property:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="cp">#import &quot;iPhoneInsomniaAppDelegate.h&quot;</span>
</span><span class='line'><span class="cp">#import &quot;iPhoneInsomniaViewController.h&quot;</span>
</span><span class='line'><span class="cp">#import &quot;DeepSleepPreventer.h&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="k">@implementation</span> <span class="nc">iPhoneInsomniaAppDelegate</span>
</span><span class='line'>
</span><span class='line'><span class="k">@synthesize</span> <span class="n">window</span><span class="p">;</span>
</span><span class='line'><span class="k">@synthesize</span> <span class="n">viewController</span><span class="p">;</span>
</span><span class='line'><span class="k">@synthesize</span> <span class="n">deepSleepPreventer</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now modify <code>-applicationDidFinishLaunching:</code> to look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">applicationDidFinishLaunching:</span><span class="p">(</span><span class="n">UIApplication</span> <span class="o">*</span><span class="p">)</span><span class="nv">application</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// Here we create our deepSleepPreventer and get it to keep our iPhone from deep sleeping</span>
</span><span class='line'>    <span class="n">self</span><span class="p">.</span><span class="n">deepSleepPreventer</span> <span class="o">=</span> <span class="p">[[</span><span class="n">DeepSleepPreventer</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">deepSleepPreventer</span> <span class="n">startPreventSleep</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">[</span><span class="n">window</span> <span class="nl">addSubview:</span><span class="n">viewController</span><span class="p">.</span><span class="n">view</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">window</span> <span class="n">makeKeyAndVisible</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>And finally the rest of &#8220;iPhoneInsomniaAppDelegate.m&#8221;. Release our property in <code>-dealloc</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">dealloc</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">deepSleepPreventer</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">viewController</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">window</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">super</span> <span class="n">dealloc</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Don&#39;t forget the matching @end to our @implementation</span>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Well, that&#8217;s it! We are done! :)</p>

<h2>Use This in Your Own App</h2>

<p>Just add the two frameworks &#8220;AVFoundation.framework&#8221; and &#8220;AudioToolbox.framework&#8221; to your own app, add the sound file &#8220;noSound.wav&#8221; and the files &#8220;DeepSleepPreventer.h&#8221; and &#8220;DeepSleepPreventer.m&#8221; to it and instantiate and use an object of the &#8220;DeepSleepPreventer&#8221; class where it fits best, and you&#8217;re ready to go.</p>

<h2>License</h2>

<p>The source code of this how-to is licensed under the New BSD License. Here comes the license text:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>Copyright (c) 2009-2010, Marco Peluso - marcopeluso.com
</span><span class='line'>All rights reserved.
</span><span class='line'>
</span><span class='line'>Redistribution and use in source and binary forms, with or without
</span><span class='line'>modification, are permitted provided that the following conditions are met:
</span><span class='line'>
</span><span class='line'>  1. Redistributions of source code must retain the above copyright notice,
</span><span class='line'>     this list of conditions and the following disclaimer.
</span><span class='line'>
</span><span class='line'>  2. Redistributions in binary form must reproduce the above copyright
</span><span class='line'>     notice, this list of conditions and the following disclaimer in the
</span><span class='line'>     documentation and/or other materials provided with the distribution.
</span><span class='line'>
</span><span class='line'>  3. Neither the name of the copyright holders nor the names of its
</span><span class='line'>     contributors may be used to endorse or promote products derived from
</span><span class='line'>     this software without specific prior written permission.
</span><span class='line'>
</span><span class='line'>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
</span><span class='line'>AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
</span><span class='line'>THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
</span><span class='line'>PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
</span><span class='line'>CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
</span><span class='line'>EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
</span><span class='line'>PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
</span><span class='line'>OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
</span><span class='line'>WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
</span><span class='line'>OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
</span><span class='line'>ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span></code></pre></td></tr></table></div></figure>


<h2>Issues and Epilog</h2>

<p>I couldn&#8217;t start my <code>MPMusicPlayer</code> in my music alarm clock app, while I was playing the silent sound with the AVAudioPlayer. So I&#8217;m guessing, it&#8217;s not possible to play other sounds while this sound plays, at least not via the <code>MPMediaPlayer</code> class.</p>

<p>As I didn&#8217;t use any other sounds in my applications yet, I don&#8217;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.</p>

<p>Also, as Shabbir Vijapura pointed out in a <a href="http://blog.marcopeluso.com/2009/07/28/timers-and-locked-screensleep-issues/comment-page-1/#comment-7">comment</a> to <a href="http://blog.marcopeluso.com/2009/07/28/timers-and-locked-screensleep-issues/">my previous post</a> there might be an easier way, to run timers on the iPhone, while it is asleep. I haven&#8217;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.</p>

<p><strong>Update: Apparently this method, doesn&#8217;t work, as Shabbir pointed out in the comments.</strong></p>

<p>Furthermore, if you have any experience yourself, dealing with the iPhone&#8217;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.</p>

<p>Thanks for reading. I hope this is useful to someone out there.</p>

<h2>Downloads</h2>

<p>You can download a drop-in piece of code and the silent sound file from GitHub: <a href="http://github.com/marcop/MMPDeepSleepPreventer">MMPDeepSleepPreventer on Github</a></p>

<p>Or you can download the whole demo project, including all source code and the silent sound file: <a href="http://github.com/marcop/iPhoneInsomnia">iPhoneInsomnia on GitHub</a></p>

<p>All the code is licensed under the New BSD License. Feel free, to use it in your own apps. Some attribution would be appreciated.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Timers and Locked Screen/Sleep Issues]]></title>
    <link href="http://blog.marcopeluso.com/2009/07/28/timers-and-locked-screensleep-issues/"/>
    <updated>2009-07-28T14:57:00+02:00</updated>
    <id>http://blog.marcopeluso.com/2009/07/28/timers-and-locked-screensleep-issues</id>
    <content type="html"><![CDATA[<p>We all know, on the iPhone and iPod touch there are certain limitations for us developers.</p>

<p>The first one I&#8217;ve encountered while developing my second app “TuneAlarm”, was that it&#8217;s not possible to execute any timers while an app is active, but the iPhone/iPod touch is in sleep mode (that is, the screen is turned off/locked).</p>

<p>The app is basically an alarm clock, that wakes you with your favorite song straight out of your iPod Music Library.</p>

<p>Of course I knew it isn&#8217;t possible to run your app in the background, but I thought this wouldn&#8217;t be a problem for TuneAlarm, as long as the user doesn&#8217;t switch to another application. I didn&#8217;t have deep sleep on my bill.</p>

<p>Worst of all, I just noticed this at the final test runs of my app, when I thought it was ready for release. The reason for that is, that the iPhone apparently sleeps differently, while being connected via the Dock Connector. This sleep doesn&#8217;t seem to be as deep as the one, when the iPhone is not connected via the Dock Connector. You can run your timers just fine, while the screen is turned off and your iPhone is getting juice over its Dock Connector.</p>

<p>I didn&#8217;t test my app without my iPhone being connected before, so this came as a little shock to me. Was the work invested in my second app worthless now? Was it all for nothing?</p>

<p>I began thinking about possible solutions: Push Notifications first came to mind. So I thought about, how my problem could be solved with the help of Push Notifications.</p>

<p><em>Imagine this: iPhone is in sleep mode. Push Notification arrives.</em></p>

<p>What happens next? The notification cannot activate your app, or call a method, or something like that. The user has to tap on the message and unlock the screen. Not until then, your app can react and do something.</p>

<p>This was obviously not a solution for TuneAlarm, as I cannot expect my sleeping user to do all that. So it was clear, I wouldn&#8217;t use Push Notifications without even taking into account, that I would have to run my own server to be able to use them or possible network issues, that could render my alarm unreliable.</p>

<p>I googled and searched the devforums for another solution and found some posts about disabling the application&#8217;s idle timer via <code>[[UIApplication sharedApplication] setIdleTimerDisabled:YES]</code> that would result in the application not falling asleep, as long as the user doesn&#8217;t put his or her device to sleep manually. You could run your normal timers then, they would fire as expected.</p>

<p>This has certain downsides as well:</p>

<ul>
<li>The screen stays on the whole time. -> Battery life is quite bad.</li>
<li>If the user (accidently) puts his or her device to sleep, you&#8217;re screwed. Your timers won&#8217;t fire.</li>
</ul>


<p>I figured, these downsides are unpleasent, but I didn&#8217;t find any better solution, so I gave it a try.</p>

<p>I disabled the application&#8217;s idle timer, compiled and tested my app again (without my iPhone plugged in via USB). Now, the screen shouldn&#8217;t go black anymore, unless I put my iPhone to sleep manually and my timers should fire correctly, right?</p>

<p>Reality was different. After a minute of not touching my iPhone, the screen went black. My timers wouldn&#8217;t fire anymore. <em>BUMMER!</em></p>

<p>Turns out, disabling the application&#8217;s idle timer did work quite reliable with iPhone OS 2.2.1, but is unreliable with iPhone OS 3.0.</p>

<p><strong>UPDATE #1: Following workaround doesn&#8217;t really work. I just got lucky a few times in a row, while testing this out. I guess. Further testing revealed, that disabling the application&#8217;s idle timer simply doesn&#8217;t achieve any reliable results, when running iPhone OS 3.0. Sorry to disappoint. :(</strong></p>

<p><del>After some testing and fiddling around, I think I got it to work. At least all of my testing implies that my workaround is working. (Excuse my overuse of “work” in the last 2 sentences, please.)</del></p>

<p><del>Here&#8217;s how I&#8217;ve done it: My app contains an <code>MPMusicPlayerController</code>. At the end of my app&#8217;s MainViewController&#8217;s <code>-viewDidLoad</code> method I just call <code>[musicPlayer play]</code> and <code>[musicPlayer stop]</code> after initialising the player.</del></p>

<p><del>Here&#8217;s the complete code I wrote to get it all working:</del></p>

<p><del><code>There was some code here, that has been removed.</code></del></p>

<p><del>Finally I decided to go with this solution and advise my users to connect their iPhones to their USB charger or dock, while using this app, as this helps against the downsides of bad battery life or accidently locking the screen manually. It&#8217;s the best I can do for now, though it&#8217;s not a 100% satisfying.</del></p>

<p><strong>Update #2: So I&#8217;m still not ready to release this app. I did some more digging and came across some audio hack, that involved playing a sound file silently in 15 second intervalls to keep the iPhone from going to deep sleep. I haven&#8217;t tested it, yet, though. I&#8217;ll post another update on this, tomorrow.</strong></p>

<p><strong>Update #3: Today, I tried out the “audio hack”. It seems to work reliably. The iPhone appears to be going to deep sleep after 10 seconds, so you have to play a silent sound at least every ten seconds, with a correctly set up audio session.
I don&#8217;t know the exact impact on battery life, yet. I will test this tonight.</strong></p>

<p><strong>I will post a complete tutorial tomorrow.</strong></p>

<p>I hope Apple changes the sleep behavior and at least enables us developers to run some timers in sleep mode respectively enables apps to set the iPhone&#8217;s sleep mode to some sort of a “semi sleep mode” if they need some background functionality, while the iPhone&#8217;s screen is locked or finally give us some (even limited) background process functionality.</p>

<p>There certainly is some sort of way for Apple itself to do this as we can see on the phone app: You are able to recieve calls, even if your iPhone is asleep.</p>

<p><strong>What do you think about this? Did you encounter some of the same problems I have? How did you deal with it?</strong></p>

<p>PS: If you could point me to some WordPress themes with better code displaying abilities, I would appreciate it. :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hi There! :)]]></title>
    <link href="http://blog.marcopeluso.com/2009/07/26/hi-there/"/>
    <updated>2009-07-26T02:46:00+02:00</updated>
    <id>http://blog.marcopeluso.com/2009/07/26/hi-there</id>
    <content type="html"><![CDATA[<p>Thanks for checking by and welcome to my website!</p>

<p>My name is Marco Peluso and I&#8217;m an indie software developer. I live in Germany near Frankfurt am Main.</p>

<p>I mainly create apps for iPhone and OS X.</p>

<p>My Twitter name is <a href="http://twitter.com/marco_peluso">@marco_peluso</a>. I tweet about my apps, the process of creating them and just random stuff. You&#8217;re invited to follow me.</p>

<p>I&#8217;m planning on blogging about my development experiences, as well as about everything I may find relevant or simply entertaining, so stay tuned. There&#8217;s much more to come!</p>

<p>Hope to see you around. :)</p>
]]></content>
  </entry>
  
</feed>
