<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cocoa Is My Girlfriend &#187; Undocumented</title>
	<atom:link href="http://www.cimgf.com/category/undocumented/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cimgf.com</link>
	<description>Taglines are for Windows programmers</description>
	<lastBuildDate>Thu, 15 Jul 2010 21:20:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The journey to disabling sleep with IOKit</title>
		<link>http://www.cimgf.com/2009/10/14/the-journey-to-disabling-sleep-with-iokit/</link>
		<comments>http://www.cimgf.com/2009/10/14/the-journey-to-disabling-sleep-with-iokit/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 18:30:42 +0000</pubDate>
		<dc:creator>Fraser Hess</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Undocumented]]></category>

		<guid isPermaLink="false">http://www.cimgf.com/?p=760</guid>
		<description><![CDATA[If your app is fullscreen, like a game, has a presentation mode, or plays long running movie files, you&#8217;ll want to disable the display from sleeping. DVD Player and Keynote are perhaps the two most obvious examples of this functionality. The documentation for this is a little spotty so here&#8217;s the results of my investigation. [...]]]></description>
			<content:encoded><![CDATA[<p>If your app is fullscreen, like a game, has a presentation mode, or plays long running movie files, you&#8217;ll want to disable the display from sleeping.  DVD Player and Keynote are perhaps the two most obvious examples of this functionality.
<span id="more-760"></span>
The documentation for this is a little spotty so here&#8217;s the results of my investigation.  My initial googling found this snippet from <a href="http://developer.apple.com/mac/library/qa/qa2004/qa1160.html">here</a>.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">UpdateSystemActivity<span style="color: #002200;">&#40;</span>OverallAct<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>


<p>The docs explain that you fire this every 30 seconds.  On the surface, this looks like a hack and well, I don&#8217;t need a Carbon C call for that, I can fake a mouse event every 30 seconds.</p>

<p>This is out for good anyway, given that it doesn&#8217;t compile on Snow Leopard on x86_64. Carbon 64-bit will never arrive.</p>

<p>Turns out that the modern way to disable sleep uses IOKit. Apple has <a href="http://developer.apple.com/mac/library/qa/qa2004/qa1340.html">a doc</a> for that too. Here is Listing 2:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;IOKit/pwr_mgt/IOPMLib.h&gt;</span>
&nbsp;
...
<span style="color: #11740a; font-style: italic;">// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,</span>
<span style="color: #11740a; font-style: italic;">// kIOPMAssertionTypeNoIdleSleep prevents idle sleep</span>
&nbsp;
IOPMAssertionID assertionID;
IOReturn success <span style="color: #002200;">=</span> IOPMAssertionCreate<span style="color: #002200;">&#40;</span>kIOPMAssertionTypeNoDisplaySleep, 
                                    kIOPMAssertionLevelOn, <span style="color: #002200;">&amp;</span>assertionID<span style="color: #002200;">&#41;</span>; 
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>success <span style="color: #002200;">==</span> kIOReturnSuccess<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">//Add the work you need to do without </span>
    <span style="color: #11740a; font-style: italic;">//  the system sleeping here.</span>
&nbsp;
    success <span style="color: #002200;">=</span> IOPMAssertionRelease<span style="color: #002200;">&#40;</span>assertionID<span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">//The system will be able to sleep again. </span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>


<p>With any sample code that I&#8217;m gonna try to use in my apps, I take the time to understand what&#8217;s going on, and to read the docs for that with which I&#8217;m not familiar.  (Don&#8217;t you?  That&#8217;s fine, just throw someone else&#8217;s potential garbage in your app.  I&#8217;m sure it won&#8217;t bite until you least expect it.)</p>

<p>I&#8217;m getting back an <code>IOPMAssertionID</code>, so I proceed to look that up in the XCode documentation viewer. And there&#8217;s nothing to be found. A look in IOPMLib.h tells me it&#8217;s a 32-bit unsigned integer and therefore not very interesting.</p>

<p>So, I move on to look up <code>IOPMAssertionCreate()</code> to find that it was introduced in Leopard only to be deprecated in Snow Leopard in favor of <code>IOPMAssertionCreateWithName()</code>. <code>IOPMAssertionCreateWithName()</code> has an extra parameter over <code>IOPMAssertionCreate()</code>, and it&#8217;s a name as you might imagine. I don&#8217;t care for having deprecated calls in my code so <code>IOPMAssertionCreateWithName()</code> it is. <strong>Caveat</strong>: <code>IOPMAssertionCreateWithName()</code> is not <em>publicly</em> available in Leopard, so either use <code>IOPMAssertionCreate()</code> in Xcode3.1/Leopard or compile in XCode 3.2/Snow Leopard and <code>IOPMAssertionCreateWithName()</code> will work when run on Leopard.</p>

<p>The sample code from Apple lists two assertion types that you can use: <code>kIOPMAssertionTypeNoDisplaySleep</code> that prevents display sleep, and <code>kIOPMAssertionTypeNoIdleSleep</code> that prevents idle sleep.</p>

<p>My testing indicates that <code>kIOPMAssertionTypeNoDisplaySleep</code> actually prevents both display and idle sleep. The intended use case for this is clearly along the lines of playing a movie or running a presentation.</p>

<p><code>kIOPMAssertionTypeNoIdleSleep</code> will still allow the display to sleep, but the Mac never sleeps unless it runs out of battery power.  Use cases for this fall into the category of &#8216;computations that may not finish before the computer sleeps&#8217;, such as bouncing an mixed audio/video file to disk or rendering a 3D image, etc.</p>

<p>Here is an <a href='http://www.cimgf.com/wp-content/uploads/2009/10/DisableSleep.zip'>example project</a> that allows you to test both behaviors. <strong>Note:</strong> The project uses <code>IOPMAssertionCreate()</code> and not <code>IOPMAssertionCreateWithName()</code> in order to compile on both Leopard and Snow Leopard.</p>

<p>In keeping with good practice, I have filed bug reports with Apple for pieces of the documentation that I see as missing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cimgf.com/2009/10/14/the-journey-to-disabling-sleep-with-iokit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Magical iPhone View Controllers</title>
		<link>http://www.cimgf.com/2009/05/11/magical-iphone-view-controllers/</link>
		<comments>http://www.cimgf.com/2009/05/11/magical-iphone-view-controllers/#comments</comments>
		<pubDate>Mon, 11 May 2009 16:19:53 +0000</pubDate>
		<dc:creator>Matt Long</dc:creator>
				<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Undocumented]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPod Touch]]></category>

		<guid isPermaLink="false">http://www.cimgf.com/?p=531</guid>
		<description><![CDATA[Update: This is documented behavior. Every now and again while doing development you stumble upon something that makes you go, hmmmm. Those are normally the moments at which you have to ask yourself, &#8220;is this a bug or a feature&#8221;. If it&#8217;s a bug, then you should file a radar with Apple, however, what if [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: This is <a href="http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:">documented behavior</a></strong>.</p>

<p>Every now and again while doing development you stumble upon something that makes you go, hmmmm. Those are normally the moments at which you have to ask yourself, &#8220;is this a bug or a feature&#8221;. If it&#8217;s a bug, then you should file a radar with Apple, however, what if it&#8217;s a feature? You blog about it, of course!</p>

<p>I have done a bit less iPhone development than Marcus, so he was a little stumped while looking through some of my code where I created a view controller using a simple alloc/init. Most interestingly is that fact that the app works. It loads the correct nib and displays the view just fine without any trouble. Notice I said alloc/init and not alloc/initWithNibName. How can this possibly work? How did my controller &#8220;know&#8221; which view to use?
<span id="more-531"></span></p>

<p>Well, the answer is pretty simple, but not immediately obvious. I could try to act like I meant to use an undocumented feature because I&#8217;m just that cool, but the fact of the matter is I was in band. Marching band, even. Band people aren&#8217;t cool. Though the color guard thought we were cool. But I digress. I just inadvertently added the alloc/init code without thinking and never looked back since the view loaded and displayed just fine. After renaming the nib/xib in the project and in doing so causing the view to fail to load properly we were able to determine that there must be some underlying <em>feature</em> of view controllers where it will automagically look for a view based upon its own name.</p>

<h2>Say Huh?</h2>

<p>Yep. That&#8217;s correct. If you create a view controller with the name <em>MySpiffyViewController</em>, for example, and instantiate it with alloc/init in your code, it will assume that the nib you want is actually called <em>MySpiffyView</em>. Spiffy eh? Some people may call this a bug, but the fact that it works this way shows that it was intentional. Now, I recommend that you steer clear of intentionally creating your view controllers this way as Apple may decide it&#8217;s a bug after all and change (fix?) it in a future release. It is however an interesting undocumented feature at the moment.</p>

<h2>You Can Duplicate It</h2>

<p>Here are the steps if you want to try this yourself. Of course, you can just <a href="#example">download the example project below</a> if you don&#8217;t want to go through the steps.</p>

<ol>
<li>In Xcode, press Command+Shift+N to open the New Project templates and select <strong>View-based Application</strong> in the <strong>iPhone OS</strong> category and click <strong>Choose&#8230;</strong>.</li>
<br />
<li>Name the project <strong>SpiffyView</strong> and click <strong>Save</strong></li>
<br />
<li>The first thing we need to do is change our code in the app delegate (called SpiffyViewAppDelegate.m) to add a UINavigationController.


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>    
    UINavigationController <span style="color: #002200;">*</span>navController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationController alloc<span style="color: #002200;">&#93;</span> initWithRootViewController<span style="color: #002200;">:</span>viewController<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>navController navigationBar<span style="color: #002200;">&#93;</span> setBarStyle<span style="color: #002200;">:</span>UIBarStyleBlackTranslucent<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>navController view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>



This will enable us to navigate to the new view we create next.
</li>
<br />
<li>Press Command+N and select <strong>View XIB</strong> under the <strong>User Interfaces</strong> category. Click <strong>Next</strong>. Name the file <strong>MySpiffyView</strong> and click <strong>Finish</strong></li>
<br />
<li>Press Command+N again and select <strong>UIViewController subclass</strong> under the <strong>Cocoa Touch Classes</strong> category. Click <strong>Next</strong>. Name the file <strong>MySpiffyViewController.m</strong> and click <strong>Finish</strong>.</li>
<br />
<li>Next we need to create an action in the view controller created by the project template called SpiffyViewViewController. Add the following code to the respective .m and .h files as follows.


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Add this to your SpiffyViewViewController.h</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showSpiffyView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Add this to your SpiffyViewViewController.m. Make sure you #import &quot;MySpiffyViewController.h&quot; as well.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showSpiffyView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MySpiffyViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self navigationController<span style="color: #002200;">&#93;</span> pushViewController<span style="color: #002200;">:</span>controller animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>controller release<span style="color: #002200;">&#93;</span>, controller <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>


</li>
<br />
<li>Now we need to connect a button to the action. In your project under resources, double-click SpiffyViewViewController.xib to open it in Interface Builder.</li>
<br />
<li>In Interface Builder, drag a button onto the view and change its text to &#8220;Spiffy View&#8221;</li>
<br />
<li>Now, Control-Click and drag a connection from your button in the view, to the <strong>File&#8217;s Owner</strong> and select <strong>showSpiffyView:</strong> in the ensuing menu. Save the file in IB.</li>
<br />
<li>Now, back in Xcode, double click MySpiffyView.xib to open it in Inteface Builder.</li>
<br />
<li>Change the <strong>File&#8217;s Owner</strong> class to MySpiffyViewController in the <strong>Identity</strong> tab of the <strong>Inspector</strong>.</li>
<br />
<li>Drag a label onto the view. Double click it to edit it and change the text to &#8220;This is the super spiffy view.&#8221; Save the file in IB.</li>
<br />
<li>Everything should be hooked up now. Go back to Xcode and &#8220;Build &amp; Go&#8221;. Your app should load in the simulator. When you see the &#8220;Spiffy View&#8221; button, click it and it should show your new view!!</li>
<br />
</ol>

<h2>Conclusion</h2>

<p>So how is that working? The only answer is there must be some underlying code that the view controller is using to locate its nib. Remember, we never told our controller what nib to use. We simply called alloc/init and voila, it works!!</p>

<p>If you think I ought to register this as a bug with Apple, let me know your thoughts in the comments. Oh and one other thing. I may have been in band, but I married a smokin&#8217; hot cheerleader. Take that all you cool people. ;-) Until next time.
<br /><br /><br />
<a name="example">
<a href='http://www.cimgf.com/wp-content/uploads/2009/05/spiffyview.zip'>Example Project SpiffyView.zip</a>
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cimgf.com/2009/05/11/magical-iphone-view-controllers/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
