<?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>A Tasty Pixel » Blog &#187; iPhone</title>
	<atom:link href="http://atastypixel.com/blog/tag/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://atastypixel.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 19 Jul 2010 10:22:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Making UIToolbar and UINavigationBar&#8217;s background totally transparent</title>
		<link>http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/</link>
		<comments>http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 10:14:02 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1959</guid>
		<description><![CDATA[Technique to make the background of UIToolbar and UINavigationBar transparent, for custom interfaces]]></description>
			<content:encoded><![CDATA[<p>I have an upcoming iPhone application, <a href="http://atastypixel.com/products/cartographer">Cartographer</a>, that is highly stylised and requires high customisation of the interface to achieve a convincing, beautiful vintage look.  To make it work, I needed transparent toolbars and navigation bars for my UIViewController-based views.</p>

<p>The solution I came up with for this was to implement a category on UINavigationBar and UIToolbar, and overriding <code>drawRect:</code> with a method that does absolutely nothing.  Then I can place my own textures behind the bar, and they&#8217;ll be seen, instead of the default bar background.<span id="more-1959"></span></p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> UINavigationBar <span style="color: #002200;">&#40;</span>TransparentAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">@end</span>
<span style="color: #a61390;">@implementation</span> UINavigationBar <span style="color: #002200;">&#40;</span>TransparentAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Do nothing!</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>


<p>For UIToolBar, if you&#8217;re using it within a UINavigationController, you&#8217;ll want to also override <code>drawLayer:inContext:</code>, as this appears to be used instead of <code>drawRect:</code> when used within a navigation controller, for some weird reason.</p>

<p>Note that this method will affect <em>all</em> bars in your app. If you only want <em>some</em> bars to be transparent, you&#8217;ll need to do a little objc-hocus-pocus.  Thanks to Mike Ash for this solution on <a href="http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html">method replacement</a> (read that article for the whys and hows).  This technique replaces the default methods as before, but keeps track of the defaults.  If you now set the <code>tintColor</code> of the bar to <code>[UIColor clearColor]</code>, the bar will have a transparent background.  Otherwise, it&#8217;ll just look the same as usual.</p>

<p>For UIToolbar (same principle for UINavigationBar):</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;objc/runtime.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Keep track of default implementation</span>
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">*</span>_origDrawRect<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">SEL</span>, CGRect<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">*</span>_origDrawLayerInContext<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">SEL</span>, CALayer<span style="color: #002200;">*</span>, CGContextRef<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Override for drawRect:</span>
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> OverrideDrawRect<span style="color: #002200;">&#40;</span>UIToolbar <span style="color: #002200;">*</span>self, <span style="color: #a61390;">SEL</span> _cmd, CGRect r<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self tintColor<span style="color: #002200;">&#93;</span> isEqual<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Do nothing</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Call default method</span>
        _origDrawRect<span style="color: #002200;">&#40;</span>self, _cmd, r<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Override for drawLayer:inContext:</span>
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> OverrideDrawLayerInContext<span style="color: #002200;">&#40;</span>UIToolbar <span style="color: #002200;">*</span>self, <span style="color: #a61390;">SEL</span> _cmd, CALayer <span style="color: #002200;">*</span>layer, CGContextRef context<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self tintColor<span style="color: #002200;">&#93;</span> isEqual<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Do nothing</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Call default method</span>
        _origDrawLayerInContext<span style="color: #002200;">&#40;</span>self, _cmd, layer, context<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> UIToolbar <span style="color: #002200;">&#40;</span>TransparentAdditions<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>load <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Replace methods, keeping originals</span>
    Method origMethod <span style="color: #002200;">=</span> class_getInstanceMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    _origDrawRect <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>method_getImplementation<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>class_addMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawRect, method_getTypeEncoding<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
        method_setImplementation<span style="color: #002200;">&#40;</span>origMethod, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawRect<span style="color: #002200;">&#41;</span>;
&nbsp;
    origMethod <span style="color: #002200;">=</span> class_getInstanceMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawLayer<span style="color: #002200;">:</span>inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    _origDrawLayerInContext <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>method_getImplementation<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>class_addMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawLayer<span style="color: #002200;">:</span>inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawLayerInContext, method_getTypeEncoding<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
        method_setImplementation<span style="color: #002200;">&#40;</span>origMethod, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawLayerInContext<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<p>You can now add background texture to the bars in a number of ways. These are two I&#8217;ve used:</p>

<ul>
<li>By adding a CALayer to <code>[bar layer]</code> &#8212; but note that UINavigationBar will try to add elements at index 0, underneath your background.  To make this work, I provided a subclassed CALayer (and overrode UINavigationBar&#8217;s <code>+layer</code> method) which only lets <em>you</em> insert layers at index 0, via a custom method, and override <code>insertLayer:atIndex:</code> method, setting index to 1 if it&#8217;s 0.  UIToolbar doesn&#8217;t require this.</li>
<li>Or, by adding a CALayer to your view layer.  Note that the view&#8217;s bounds do not cover the UINavigationBar; I had to offset the layer by the height of the bar in question (<code>navigationBarLayer.frame = CGRectMake(0, -self.navigationController.navigationBar.frame.size.height, [barImage size].width, [barImage size].height);</code>, for example), and set <code>self.view.clipsToBounds = NO</code> to allow the layer to be seen.</li>
</ul>

<p>Of course, you can also draw the texture in <code>drawRect:</code>, instead.  It&#8217;s entirely up to you.  The advantage in using a <code>CALayer</code> is that it can overlap the view boundary, for effects like drop shadows.</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/07/201007191118.jpg" width="320" height="84" alt="201007191118.jpg" class="aligncenter" /></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1959" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>UIImage, resolution independence and the iPhone 4&#8242;s Retina display</title>
		<link>http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/</link>
		<comments>http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 13:43:19 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1951</guid>
		<description><![CDATA[iOS4 caters for the high-resolution Retina display that comes with the iPhone 4 by some rather clever abstraction, that moves away from the concept of &#8216;pixels&#8217;, and instead uses &#8216;points&#8217;, which are resolution-independent. So, when you display an image that&#8217;s been prepared for the Retina display, it&#8217;s represented with a scale factor of 2, meaning [...]]]></description>
			<content:encoded><![CDATA[<p>iOS4 caters for the high-resolution Retina display that comes with the iPhone 4 by some rather clever abstraction, that moves away from the concept of &#8216;pixels&#8217;, and instead uses &#8216;points&#8217;, which are resolution-independent.</p>

<p>So, when you display an image that&#8217;s been prepared for the Retina display, it&#8217;s represented with a scale factor of 2, meaning that to your code, it appears to have the same dimensions, but in fact contains twice the information density.</p>

<p>iOS4&#8242;s UIImage makes it work by automatically looking for high-res images located alongside the prior &#8216;standard resolution&#8217; ones &#8212; identified by a &#8220;@2x&#8221; suffix to the filename.</p>

<p>This works great with <code>+[UIImage imageNamed:]</code>, but although the <a href="http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/SupportingResolutionIndependence/SupportingResolutionIndependence.html#//apple_ref/doc/uid/TP40007072-CH10-SW8">API documentation</a> says that other image loading methods will automatically load the @2x versions, they actually don&#8217;t.  Yeah.  Apple are working on it.</p>

<p>Until they sort themselves out, I&#8217;m using a convenience method sitting inside a UIImage category.  So, where I would previously use something like <code>[UIImage imageWithContentsOfFile:]</code>, I now use <code>[UIImage imageWithContentsOfResolutionIndependentFile:]</code>.<span id="more-1951"></span>Here&#8217;s the category:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithContentsOfResolutionIndependentFile<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>path;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageWithContentsOfResolutionIndependentFile<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>path;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithContentsOfResolutionIndependentFile<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>path <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>UIScreen instancesRespondToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>scale<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> scale<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">2.0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path2x <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>path stringByDeletingLastPathComponent<span style="color: #002200;">&#93;</span> 
                            stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@@2x.%@&quot;</span>, 
                                                            <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>path lastPathComponent<span style="color: #002200;">&#93;</span> stringByDeletingPathExtension<span style="color: #002200;">&#93;</span>, 
                                                            <span style="color: #002200;">&#91;</span>path pathExtension<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span> fileExistsAtPath<span style="color: #002200;">:</span>path2x<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self initWithContentsOfFile<span style="color: #002200;">:</span>path2x<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self initWithContentsOfFile<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageWithContentsOfResolutionIndependentFile<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>path <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage alloc<span style="color: #002200;">&#93;</span> initWithContentsOfResolutionIndependentFile<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<p>This checks to see whether the iOS4 features are present, so this code should work on devices running prior OS versions as well.</p>

<p>What iOS4&#8242;s <code>imageWithContentsOfFile</code> <em>does</em> do is recognise if an image is the &#8217;2x&#8217; version, and sets the <code>scale</code> accordingly, so it displays correctly.</p>

<p><img src="http://atastypixel.com/blog/wp-content/michaelangelo-images/edf7ea2c77f9c2935e3c8c803e2bc112.jpg" width="482" height="281" alt="Before and After resolution independence" class="aligncenter frame" /></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1951" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Links for February 25th through May 29th</title>
		<link>http://atastypixel.com/blog/links-february-25th-may-29th/</link>
		<comments>http://atastypixel.com/blog/links-february-25th-may-29th/#comments</comments>
		<pubDate>Sat, 29 May 2010 22:01:21 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[Marketing]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1849</guid>
		<description><![CDATA[Links for February 25th through May 29th: Implementing iBooks page curling using a conical deformation algorithm Excellent summary of how to implement a convincing page turn animation in OpenGL Multiplottr.com &#8212; Plot, save and share multiple locations on your own customized maps. Batch plot multiple addresses gmaps.kaeding.name :: Plot multiple locations on Google Maps Enter [...]]]></description>
			<content:encoded><![CDATA[<p>Links for February 25th through May 29th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://wdnuon.blogspot.com/2010/05/implementing-ibooks-page-curling-using.html">Implementing iBooks page curling using a conical deformation algorithm</a> Excellent summary of how to implement a convincing page turn animation in OpenGL</li>
<li><a href="http://www.multiplottr.com/">Multiplottr.com &#8212; Plot, save and share multiple locations on your own customized maps.</a> Batch plot multiple addresses</li>
<li><a href="http://gmaps.kaeding.name/">gmaps.kaeding.name :: Plot multiple locations on Google Maps</a> Enter addresses, one per line, to plot all entries on a map at once</li>
<li><a href="http://www.onlinemarketingrant.com/free-iphone-app-marketing">Free iPhone App Marketing &mdash; Online and iPhone Marketing</a> Includes a useful list of review sites, blogs, etc.</li>
</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1849" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-february-25th-may-29th/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Making of Talkie: Multi-interface broadcasting and multicast</title>
		<link>http://atastypixel.com/blog/the-making-of-talkie-multi-interface-broadcasting-and-multicast/</link>
		<comments>http://atastypixel.com/blog/the-making-of-talkie-multi-interface-broadcasting-and-multicast/#comments</comments>
		<pubDate>Sun, 02 May 2010 12:52:38 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Broadcast]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Multicast]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Talkie]]></category>
		<category><![CDATA[Talkie-for-Mac]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1901</guid>
		<description><![CDATA[Part 2 Talkie is my newest product, a Walkie Talkie for iPhone and Mac. In Part 1 of this series, I wrote about basic broadcasting. This works fine with one network device, but it&#8217;s worth discussing how to send through all devices, so you can communicate with others connected via, say, Ethernet and WiFi simultaneously. [...]]]></description>
			<content:encoded><![CDATA[<h2>Part 2</h2>

<p><img src="http://atastypixel.com/media/images/products/talkie/icon-medium.jpg" width="183" height="148" alt="Talkie" style="float:right;" /><a href="http://atastypixel.com/products/talkie">Talkie</a> is my newest product, a Walkie Talkie for iPhone and Mac.</p>

<p>In <a href="http://atastypixel.com/blog/2010/03/11/the-making-of-talkie-broadcasting/">Part 1</a> of this series, I wrote about basic broadcasting.  This works fine with one network device, but it&#8217;s worth discussing how to send through all devices, so you can communicate with others connected via, say, Ethernet and WiFi simultaneously.</p>

<p>So, in Part 2 I&#8217;ll write about the approach I took in Talkie for broadcasting from all network devices (a.k.a. network interfaces), so that one can communicate with others connected via WiFi, Ethernet (on a Mac), and any other network devices simultaneously.</p>

<p><span id="more-1901"></span></p>

<h2>Bind them</h2>

<p>From Part 1, we have a <a href="http://atastypixel.com/blog/2010/03/11/the-making-of-talkie-broadcasting/">working broadcast mechanism</a>, but it will only send through the default interface &#8212; whatever you&#8217;re connected to the network via.  This is often sufficient, but if you have more than one device that you communicate through, like Ethernet and WiFi, then you will find that it only works with one.</p>

<p>In order to send through <em>all</em> your connected network interfaces, we need to create one socket for each interface, and <em>bind</em> the socket to its corresponding interface.</p>

<p>Here&#8217;s how:</p>

<p>First, we need to obtain a list of all network interfaces with <code>getifaddrs</code>.</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;ifaddrs.h&gt;</span>
...
<span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>addrs;
<span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> getifaddrs<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addrs<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now, <code>addrs</code> is a list of interfaces that we can iterate over.  We now do so, picking out those devices that support broadcasting, and that aren&#8217;t loopback or point-to-point devices &#8212; loopback is an internal interface that is provided for your computer&#8217;s inner dialogue, and point-to-point (ppp) devices include dialup interfaces, 3G modems and the like.  We can exclude those guys.</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">const</span> <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>cursor <span style="color: #002200;">=</span> addrs;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_BROADCAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// We will do some stuff in here</span>
&nbsp;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now, for each interface that meets our criteria, we create a socket (which we covered in <a href="http://atastypixel.com/blog/2010/03/11/the-making-of-talkie-broadcasting/">Part 1</a>), then <code>bind</code> the socket to the network interface, to force transmission from that particular device.  Finally, as we did in Part 1, we enable broadcasting using <code>setsockopt</code> with <code>SO_BROADCAST</code>.</p>

<p>We want to store the sockets we create in an array, so we can access them later.  If we assume a maximum number of interfaces we will support (lets call it <code>kMaxSockets</code>), we can just use an array of that length.  So, putting it together:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kMaxSockets 16</span>
...
<span style="color: #a61390;">int</span> sock_fds<span style="color: #002200;">&#91;</span>kMaxSockets<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">int</span> number_sockets <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&amp;&amp;</span> number_sockets &lt; kMaxSockets <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_BROADCAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create socket</span>
    sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> socket<span style="color: #002200;">&#40;</span>AF_INET, SOCK_DGRAM, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Error occurred</span>
      <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create address from which we want to send, and bind it</span>
    <span style="color: #a61390;">struct</span> sockaddr_in addr;
    <span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    addr.sin_family <span style="color: #002200;">=</span> AF_INET;
    addr.sin_addr <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr;
    addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> bind<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Error occurred</span>
      <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Enable broadcast</span>
    <span style="color: #a61390;">int</span> flag <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
    result <span style="color: #002200;">=</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, SOL_SOCKET, SO_BROADCAST, <span style="color: #002200;">&amp;</span>flag, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>flag<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Error occurred</span>
      <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    number_sockets<span style="color: #002200;">++</span>;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Finally, as before, we can setup a broadcast address to send to, and use <code>sendto</code> to broadcast, this time for each socket we created:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Initialise broadcast address</span>
<span style="color: #a61390;">struct</span> sockaddr_in addr;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> INADDR_BROADCAST;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Send through each interface</span>
<span style="color: #a61390;">int</span> i;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; i&lt;number_sockets; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> sendto<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>, data, length, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Error occurred</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Note that the receive routine only needs a single socket, as we can receive on any interface when we use <code>INADDR_ANY</code>.  So, the receive routine needs no changes from the single-interface version we saw in Part 1.</p>

<p>Here&#8217;s the test app with the above modification: <a href="http://atastypixel.com/blog/wp-content/uploads/2010/05/broadcast_sample_all_interfaces.c" title="broadcast_sample_all_interfaces.c">broadcast_sample_all_interfaces.c</a></p>

<p>Again, compile by opening Terminal, and typing <code>make broadcast_sample_all_interfaces</code> or <code>cc -o broadcast_sample_all_interfaces broadcast_sample_all_interfaces.c</code>, then run it with <code>./broadcast_sample_all_interfaces "Message to send"</code> to send, or just <code>./broadcast_sample_all_interfaces</code> with no arguments to listen.</p>

<p>You may notice that multiple messages may be received: These have probably arrived via multiple network interfaces, virtual or otherwise.  It&#8217;s usually a good idea to check for duplicate messages, if this is an issue for program operation, by including a sequence number into the message &#8212; this will be discussed in Part 3.</p>

<p>It also may be a good idea to ignore your own messages, which may find their way back to you.  One way to accomplish this is to examine the source address (<code>addr</code> in the example above) and compare it with your local interface addresses (stored in <code>addrs</code>, above).  If you get a match, the message came from you, and you can drop it.</p>

<h2>Multicast</h2>

<p>Broadcast is fine when everyone on the local network is interested in what you have to say.  If this isn&#8217;t the case, though (lets face it, those Chuck Norris jokes aren&#8217;t for everyone), effort is wasted delivering to those who aren&#8217;t particularly interested.</p>

<p>Multicast works by using a specific address that one &#8216;subscribes&#8217; to in order to receive messages sent to that address.  So, it&#8217;s opt-in, allowing for better efficiency and one day, Internet-wide support for &#8216;to-many&#8217; communications.</p>

<p>Well, in theory.  Actually, multicast is still quite new, and for the most part &#8212; from what I understand &#8212; it behaves pretty much like broadcast on a local area network.  However, support can only increase, and given that many services already use it &#8212; Multicast DNS (mDNS), also known as Bonjour, being one of the most well-known examples &#8212; it seems a good idea to follow their lead.  Note also that IPv6, the successor to IP as we know it, and our saviour-to-be from our little Internet overpopulation problem (among other things), doesn&#8217;t even have broadcast provisions &#8212; the future is all multicast.</p>

<p>So, for these reasons, Talkie speaks multicast, instead of plain ol&#8217; broadcast.</p>

<p>Making use of multicast is relatively straightforward: To receive, you join a multicast group using <code>setsockopt</code> with <code>IP_ADD_MEMBERSHIP</code>, and the address of the multicast group, which is in the range 224.0.0.0-239.255.255.255 (for IPv4, of course).  To send, you just use <code>sendto</code> to transmit data to a multicast group address.</p>

<p>Using multicast on all network interfaces is just a little more complicated.  Here&#8217;s how it&#8217;s done with Talkie:</p>

<h3>Sending</h3>

<p>The send routine is very similar to the one above, using broadcast.  However, instead of using <code>bind</code> to specify the outgoing network interface and enabling broadcast, we assign a multicast interface using <code>setsockopt</code> with <code>IP_MULTICAST_IF</code>.  And, instead of transmitting to the broadcast address, we transmit to the multicast group address.</p>

<p>Again, we loop through all network interfaces.  This time, we pick out those that support multicast (<code>ifa_flags &amp; IFF_MULTICAST</code>):</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">const</span> <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>cursor <span style="color: #002200;">=</span> addrs;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_MULTICAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// We will do some stuff in here</span>
&nbsp;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>And, after creating the socket, we assign the interface:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, IPPROTO_IP, IP_MULTICAST_IF, <span style="color: #002200;">&amp;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> in_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span>  <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Finally, as a nicety, we can disable loopback so that we don&#8217;t receive our own messages.  This isn&#8217;t 100% reliable, as certain network conditions can result in the local machine still receiving its outgoing messages, but it can improve efficiency:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">u_char loop <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, IPPROTO_IP, IP_MULTICAST_LOOP, <span style="color: #002200;">&amp;</span>loop, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>loop<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now that our sockets are set up, we can prepare to send to the multicast address:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kMulticastAddress &quot;224.0.0.123&quot;</span>
...
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> inet_addr<span style="color: #002200;">&#40;</span>kMulticastAddress<span style="color: #002200;">&#41;</span>;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;</pre></div></div>


<p>And, as before, we send through each of the sockets we created:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> i;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; i&lt;number_sockets; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sendto<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>, data, length, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Error occurred</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<h3>Receiving</h3>

<p>Receiving messages from a multicast group on several network interfaces is a little more involved than doing so with broadcast: We need to subscribe to the multicast group from each network interface, explicitly.  If we were to just specify no device in particular, the system would choose a single interface for us, neglecting the others.</p>

<p>Joining the multicast group for each interface takes place in a now-familiar loop:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">const</span> <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>cursor <span style="color: #002200;">=</span> addrs;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_MULTICAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// We will do some stuff in here</span>
&nbsp;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>For each network device, we use the <code>IP_ADD_MEMBERSHIP</code> property with <code>setsockopt</code> to join &#8212; thereby subscribing to any messages sent to the multicast group address that reach that network interface.</p>

<p>First, we prepare the join request structure.  This provides the multicast group address, and the network interface:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> ip_mreq multicast_req;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>multicast_req, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>multicast_req<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
multicast_req.imr_multiaddr.s_addr <span style="color: #002200;">=</span> inet_addr<span style="color: #002200;">&#40;</span>kMulticastAddress<span style="color: #002200;">&#41;</span>;
multicast_req.imr_interface <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr;</pre></div></div>


<p>Now we use this structure to join the group:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, <span style="color: #002200;">&amp;</span>multicast_req, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>multicast_req<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now, a caveat: While it&#8217;s perfectly legal to join the same multicast group on more than one network interface, and up to 20 memberships may be added to the same socket (see <a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man4/ip.4.html">ip(4)</a>), for some reason, OS X spews &#8216;Address already in use&#8217; errors when we actually attempt it.</p>

<p>As a workaround, we can &#8216;drop&#8217; the membership first, which would normally have no effect, as we have not yet joined on this interface.  However, it enables us to perform the subsequent join, without dropping prior memberships.</p>

<p>So, before we perform the above <code>IP_ADD_MEMBERSHIP</code>, we do a <code>IP_DROP_MEMBERSHIP</code> first:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">setsockopt<span style="color: #002200;">&#40;</span>sock_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, <span style="color: #002200;">&amp;</span>multicast_req, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>multicast_req<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>


<p>This sets up our socket to receive messages sent to the multicast group that are received via any interface.</p>

<p>Here it is all put together: <a href="http://atastypixel.com/blog/wp-content/uploads/2010/05/multicast_sample.c" title="multicast_sample.c">multicast_sample.c</a></p>

<p>Compile by opening Terminal, and typing <code>make multicast_sample</code> or <code>cc -o multicast_sample multicast_sample .c</code>, then run it with <code>./multicast_sample "Message to send"</code> to send, or just <code>./multicast_sample</code> with no arguments to listen.</p>

<h2>Still to come</h2>

<p>So, now we mostly have networking covered.  There&#8217;s one obvious omission, though, for an iPhone app: Bluetooth.  In Part 3, I&#8217;ll discuss how to perform communications over Bluetooth on the iPhone, in a way that&#8217;s compatible with the above generic network communications.  I&#8217;ll also talk about how to connect to other devices automatically, without user intervention &#8212; This is one particularly popular feature of Talkie that allows it to behave more like a real walkie-talkie.</p>

<p>I promised in Part 1 that I&#8217;d talk about packet formats.   We&#8217;ve covered a lot of ground in Part 2, however, so it shall be postponed to Part 3 &#8212; I&#8217;ll discuss how to ensure you get messages in the correct order by using sequence numbers, as well as providing for versioning and a few other bits and pieces.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1901" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/the-making-of-talkie-multi-interface-broadcasting-and-multicast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy rounded corners on UITableViewCell image view</title>
		<link>http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view/</link>
		<comments>http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 19:47:07 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2010/03/15/easy-rounded-corners-on-uitableviewcell-image-view/</guid>
		<description><![CDATA[Here&#8217;s a relatively easy way to achieve rounded corners on the standard image view in a UITableViewCell: cell.imageView.layer.masksToBounds = YES; cell.imageView.layer.cornerRadius = 5.0; Set this up when you create the cell (make sure you #import &#60;QuartzCore/QuartzCore.h&#62; at the top, of course). It would appear the UIImageView control creates sublayers to display the actual image content, [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a relatively easy way to achieve rounded corners on the standard image view in a UITableViewCell:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">cell.imageView.layer.masksToBounds <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
cell.imageView.layer.cornerRadius <span style="color: #002200;">=</span> <span style="color: #2400d9;">5.0</span>;</pre></div></div>


<p>Set this up when you create the cell (make sure you <code>#import &lt;QuartzCore/QuartzCore.h&gt;</code> at the top, of course).  It would appear the UIImageView control creates  sublayers to display the actual image content, which is why we use the <code>masksToBounds</code> property to then clip any sublayers.</p>

<p>I noticed a lot of people are seeking answers to the silly behaviour of UITableView with the grouped (<code>UITableViewStyleGrouped</code>) style and images:</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/03/201003152034.jpg" width="133" height="100" alt="Images not clipped to rounded cell border" class="aligncenter" /></p>

<p>Images don&#8217;t get clipped to the rounded cell border, which looks nasty.  This technique is one way to remedy that:</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/03/201003152036.jpg" width="130" height="73" alt="Rounded borders now stay within rounded cell edge" class="aligncenter" /></p>

<p>One caveat &#8211; due to the inexplicable way the image view within the table view cell scales image content, there&#8217;s not really a simple, sensible way to provide an inset margin from the table view cell boundary to complement this rounded border effect.</p>

<p>I tried setting the <code>frame</code> property of the UIImageView itself (<code>cell.imageView.frame</code>), as well as setting the frame of the image view&#8217;s layer.  I also tried applying a scale transform to the layer, with strangely inconsistent results: Setting scale to, say, 50%, made the image view 40x40px, only a pixel or two smaller than the full size.  This may be because another entity (the table view cell?) performs scaling of the content, instead of the actual image view; given that my original image was 80&#215;80, a 50% scale would result in 40&#215;40.</p>

<p>My solution was to steer clear of that nonsense and just provide appropriately scaled images straight to the image view.  Here&#8217;s a simple category on UIImage to scale an image:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageScaledToSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageScaledToSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size <span style="color: #002200;">&#123;</span>
    UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>size<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>self drawInRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, size.width, size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> image;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>


<p>So, then I just do something like this in the UITableView data provider:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> account.image;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> image <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   cell.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>image imageScaledToSize<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">38</span>, <span style="color: #2400d9;">38</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/03/201003152043.jpg" width="121" height="57" alt="Sorted" class="aligncenter" /></p>

<p>Sorted.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1876" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The Making of Talkie: Broadcasting</title>
		<link>http://atastypixel.com/blog/the-making-of-talkie-broadcasting/</link>
		<comments>http://atastypixel.com/blog/the-making-of-talkie-broadcasting/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 00:31:59 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Broadcast]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Talkie]]></category>
		<category><![CDATA[Talkie-for-Mac]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1854</guid>
		<description><![CDATA[Part 1 Talkie is my newest product, the result of a collaboration with a good designer friend, Tim Churchward, who did the user interface. Talkie is a little different from many of the other walkie talkie applications on the App Store (aside from the fact that much of it was written by me from our [...]]]></description>
			<content:encoded><![CDATA[<h2>Part 1</h2>

<p><img src="http://atastypixel.com/media/images/products/talkie/icon-medium.jpg" width="183" height="148" alt="Talkie" style="float:right;" /><a href="http://atastypixel.com/products/talkie">Talkie</a> is my newest product, the result of a collaboration with a good designer friend, Tim Churchward, who did the user interface.</p>

<p>Talkie is a little different from many of the other walkie talkie applications on the App Store (aside from the fact that much of it was written by me from our motorhome in <a href="http://michael.tyson.id.au/2010/01/26/down-time-in-hammamet/">Tunisia</a>!), and I thought I&#8217;d write a little about some of the tech underpinning the app, and some of the choices we made.   Along the way it may get a little tutorial-esque.</p>

<ul>
<li>This first part will introduce our initial motivations, and will talk about basic broadcast communications &#8212; the broadcast communications part may be very familiar to some, in which case it may be worth skipping to the next instalment.</li>
<li>In the <a href="http://atastypixel.com/blog/the-making-of-talkie-broadcasting/">second part</a>, I&#8217;ll continue the theme of networking, and will talk about what I ended up with for Talkie&#8217;s network code after addressing a couple of things, including switching to multicast.  </li>
<li>Finally, I&#8217;ll talk audio, dual platform development, and anything else I think of along the way (Actually, I&#8217;m aching to talk about one particular upcoming feature that had me jumping up and down when I first thought of it, but for now, mum&#8217;s the word on that one.)<span id="more-1854"></span>## Inspiration</li>
</ul>

<p><img src="http://atastypixel.com/media/images/products/talkie/screen-iphone.jpg" width="187" height="400" alt="Talkie screenshot" style="float:right;" />Right from the start, we wanted a product that brought back the fun walkie talkie experience we remember from our childhoods.  I&#8217;m talking colourful plastic, whip antennas and hiding in tree-houses.   This was mostly Tim&#8217;s domain, so I shall leave it to him to discuss how he found that in the user interface.</p>

<p>It also meant stepping back from traditional voice chat, with manual call initialisation and termination and simple one-to-one calls.  We wanted to mimic a radio, so that meant broadcasting as soon as you hit &#8220;Talk&#8221; &#8212; whereupon anyone in the neighbourhood would hear you.</p>

<p>Basically, we wanted to get as close to the real thing as was practical.  This included the addition of a prominent &#8216;morse code&#8217; button, of course, as well as a &#8216;squelch&#8217; control for &#8216;fine tuning&#8217;, which simulated the static of bad reception.</p>

<h2>Going dual-platform</h2>

<p><img src="http://atastypixel.com/media/images/products/talkie-for-mac/screenshot.jpg" width="232" height="190" alt="Talkie for Mac" style="float:left;" />Soon after I started developing Talkie, I realised I wanted a version for the Mac too.</p>

<p>Having Talkie both on the iPhone and on the Mac made sense, as we envisioned that a fairly common usage pattern would involve communication between a desktop and a handheld &#8212; say, someone wandering a campus with the iPhone in their pocket, staying in touch with friends at their desks.</p>

<p>We wanted to offer good value with Talkie, which is why we made Talkie for Mac available for free, when it&#8217;s used with Talkie for iPhone.</p>

<p>One of the very convenient things about developing for both iPhone and Mac is that the platforms are so similar, porting code is usually effortless.  So, going dual-platform was an easy decision.</p>

<h2>Finding common ground</h2>

<p>The result of all of this was the need to develop or find a communication protocol and codec that would work across the iPhone and the Mac, over both Bluetooth for iPhone-iPhone communication, and Wifi, for communication between iPhones and iPhones, and iPhones and Macs.</p>

<p>Version 3.0 of the iPhone SDK introduced the GameKit framework and as part of it, <code>GKVoiceChatService</code>, which provides two person voice chat straight out of the box.  Immediately it was clear that it wouldn&#8217;t serve our purposes &#8212; two person is not broadcast.  I was also keen to provide a Mac version of Talkie, and given that GameKit is iPhone-only, it was time to go off the beaten track.</p>

<p>There are a variety of voice communication protocols out there, with varying licences and varying complexity and feature sets.  I could&#8217;ve spent a week or two researching the options and evaluating them against our need for broadcast functionality, figuring out compilation and linking on the iPhone, resolving any compatibility or performance issues that arose, etc.</p>

<p>Or, I could spend a day putting together a few big building blocks provided by Apple (and the underlying Unix system) and have an easily tweakable working solution that precisely meets our needs and provides for future features.</p>

<p>It may not have been the most scientific approach, but the core of Talkie&#8217;s functionality was up and running on our home wi-fi network within about four hours, and it was working well.</p>

<h2>Getting from a to b (and c, d, e, and f)</h2>

<p>For those unfamiliar with the ins and outs of TCP/IP (the most common communication protocol &#8212; or rather, set of protocols &#8212; between computers, and the fundamental building block of the Internet), communication between computers can be connection-oriented, or connectionless.</p>

<p>Connection-oriented (a.k.a. &#8216;reliable&#8217;) communications are most common: They&#8217;re used when you open up a website, connect to iChat, or check your email.  This is the TCP part of TCP/IP, and includes niceties like built-in guaranteed delivery via retransmission (providing your cat hasn&#8217;t eaten your network cable, of course) and packet ordering so you receive the messages in the right order.  This only works between two computers, though &#8211; you and the server.</p>

<p>Connectionless (or &#8216;unreliable&#8217;) communications are much more open &#8212; it&#8217;s basically just a spray of messages: The data isn&#8217;t carefully ushered along, it&#8217;s just spat out and left to fend for itself.  This is UDP, and is commonly used for time-sensitive applications like audio communication, network gaming, etc., where once a packet arrives late, it&#8217;s useless: No point mucking about re-sending lost data that&#8217;s going to be past its use-by date by the time it arrives.  The other thing about UDP is that, because there&#8217;s no co-operation required from the destination (to acknowledge receipt, etc.), it lends itself to one-to-many communications &#8212; broadcast.</p>

<p>Just what we&#8217;re after.</p>

<p>So, we use connectionless communications (UDP) to send messages containing the audio, and anyone who receives them unpackages and plays the audio within.</p>

<p>The basic mechanics of this are fairly simple, once you get past the arguably cryptic C syntax.</p>

<p>Here&#8217;s how it works.</p>

<h3>Sending</h3>

<p>On the transmission end, on startup, you create a socket using the <code>socket</code> call:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #6e371a;">#include &lt;netinet/in.h&gt;</span>
<span style="color: #6e371a;">#include &lt;arpa/inet.h&gt;</span>
...
<span style="color: #a61390;">int</span> sock_fd <span style="color: #002200;">=</span> socket<span style="color: #002200;">&#40;</span>AF_INET, SOCK_DGRAM, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sock_fd &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>The socket, identified by <code>sock_fd</code>, will be created using the IPv4 domain (<code>AF_INET</code>), a &#8216;datagram&#8217; type (<code>SOCK_DFRAM</code>: that&#8217;s connectionless communication &#8212; if we wanted connection-oriented, we could put in <code>SOCK_STREAM</code> here instead), using protocol 0, which is IP.</p>

<p>Then, we enable broadcasting:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> flag <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
<span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fd, SOL_SOCKET, SO_BROADCAST, <span style="color: #002200;">&amp;</span>flag, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>flag<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>That is, we set the <code>SO_BROADCAST</code> option at the <code>SOL_SOCKET</code> level to 1 (via the <code>flag</code> variable, which we pass in as a pointer), thereby requesting permission from the operating system kernel to broadcast.</p>

<p>Now we have the carrier pigeon coop at our disposal, we can start dispatching pigeons.  First, we fill out the address:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kPortNumber 1234</span>
...
<span style="color: #a61390;">struct</span> sockaddr_in addr;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> INADDR_BROADCAST;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;</pre></div></div>


<p>(Note, <code>htons</code> converts <code>kPortNumber</code> into a format that can be understood by any computer, regardless of the way it represents numbers internally.)</p>

<p>Now, send:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>dataToSend;
result <span style="color: #002200;">=</span> sendto<span style="color: #002200;">&#40;</span>sock_fd, <span style="color: #002200;">&#91;</span>data bytes<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>data length<span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<h3>Receiving</h3>

<p>That takes care of outgoing messages.  To receive them, we want another socket:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> sock_fd <span style="color: #002200;">=</span> socket<span style="color: #002200;">&#40;</span>AF_INET, SOCK_DGRAM, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sock_fd &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now we specify where we want to receive messages from, by &#8216;binding&#8217; the socket:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> sockaddr_in addr;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> INADDR_ANY;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;
result <span style="color: #002200;">=</span> bind<span style="color: #002200;">&#40;</span>sock_fd, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>And we pass a buffer to <code>recvfrom</code> to fill up with tasty morsels of data:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kBufferSize 1024</span>
...
<span style="color: #a61390;">char</span> buffer<span style="color: #002200;">&#91;</span>kBufferSize<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">int</span> addr_len <span style="color: #002200;">=</span> <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">int</span> bytes_received <span style="color: #002200;">=</span> recvfrom<span style="color: #002200;">&#40;</span>sock_fd, buffer, kBufferSize, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span>struck sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #002200;">&amp;</span>addr_len<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> bytes_received &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Voila, we have <code>bytes_received</code> bytes sitting in <code>buffer</code> for us to do something with.  Note also that <code>addr</code> now also contains the address of the sender.  Now we can loop and continue to receive data as it comes in, passing the data off to some other part of the application to deal with.</p>

<p>We can put it all together into a simple test app: <a href="http://atastypixel.com/blog/wp-content/uploads/2010/03/broadcast_sample.c" title="broadcast_sample.c">broadcast_sample.c</a></p>

<p>Compile it by opening up Terminal, and typing <code>make broadcast_sample</code> (or <code>cc -o broadcast_sample broadcast_sample.c</code>, if you like), then run it with <code>./broadcast_sample "Message to send"</code>, or just <code>./broadcast_sample</code> to listen.</p>

<p><pre>
$ ./broadcast_sample 
Listening...
Hello world!
</pre></p>

<p><pre>
$ ./broadcast_sample 'Hello world!'
"Hello world!" transmitted.
</pre></p>

<h2>Coming next</h2>

<p>This will work happily between computers with just one network interface. But, if you have more than one (say, wireless, and an Ethernet connection too), you&#8217;ll notice that it will only communicate through one of the interfaces.  That&#8217;s because just the default interface is used.  You have to explicitly attend to each interface, to broadcast out of each one, and listen on each one.</p>

<p>In the <a href="http://atastypixel.com/blog/the-making-of-talkie-broadcasting/">next part of this series</a>, I&#8217;ll write about how I addressed that, and about multicast, which is used in things like Bonjour (MDNS).  I&#8217;ll also write about designing packet formats.</p>

<p>Thanks for reading!</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1854" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/the-making-of-talkie-broadcasting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using custom DNS servers from the iPhone and over Internet Tethering</title>
		<link>http://atastypixel.com/blog/using-custom-dns-servers-from-the-iphone-and-over-internet-tethering-3/</link>
		<comments>http://atastypixel.com/blog/using-custom-dns-servers-from-the-iphone-and-over-internet-tethering-3/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 21:01:17 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2010/01/24/using-custom-dns-servers-from-the-iphone-and-over-internet-tethering-3/</guid>
		<description><![CDATA[For those of us the roam around on network connections, OpenDNS and Google Public DNS provide public DNS servers which offer better security than using arbitrary DNS that&#8217;s assigned to us when we connect to a network. This means that rather than trusting the assigned DNS server &#8212; which could be a malicious third party [...]]]></description>
			<content:encoded><![CDATA[<p>For those of us the roam around on network connections, <a href="http://www.opendns.com">OpenDNS</a> and <a href="http://code.google.com/speed/public-dns/">Google Public DNS</a> provide public DNS servers which offer better security than using arbitrary DNS that&#8217;s assigned to us when we connect to a network.  This means that rather than trusting the assigned DNS server &#8212; which could be a malicious third party that&#8217;s attempting a man-in-the-middle attack &#8212; we always use a trusted server.</p>

<p>In OS X, normally, one can specify custom DNS servers in Network Preferences, but when using Internet Tethering with the iPhone, no options are available.</p>

<p>It&#8217;s possible to set DNS configuration on the command line, though, as mentioned in <a href="http://www.macosxhints.com/article.php?story=20050621051643993">this MacOSXHints article</a>.</p>

<p>This technique can be used within a <a href="http://stackoverflow.org/wiki/Set_the_DNS_server_under_OS_X">shell script</a> to make things easier.</p>

<p>As it happens, if you have a jailbroken iPhone, the trick works there too &#8212; just <code>ssh</code> in as root, copy the script over, and run it from the iPhone.</p>

<p>The one caveat is that the DHCP client both on the iPhone and on Mac OS X will routinely reset the servers &#8212; I haven&#8217;t found a way to combat this yet, other than routinely re-running the script.</p>

<p>We have been using mobile broadband from my iPhone while we&#8217;ve been <a href="http://michael.tyson.id.au/about">travelling</a>; our current <a href="http://michael.tyson.id.au/dk2oa">provider</a> seems to go offline almost every evening &#8212; a quirk which I&#8217;ve just discovered is related to their faulty DNS server.</p>

<p>Using Google&#8217;s public DNS servers instead fixes this problem, so I was after a way to configure both the iPhone and OS X to use the servers.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1844" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/using-custom-dns-servers-from-the-iphone-and-over-internet-tethering-3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Links for October 25th through January 23rd</title>
		<link>http://atastypixel.com/blog/links-october-25th-january-23rd/</link>
		<comments>http://atastypixel.com/blog/links-october-25th-january-23rd/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 17:01:07 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[mapping]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1684</guid>
		<description><![CDATA[Links for October 25th through January 23rd: GPS Visualizer: Draw a map from a GPS data file Batch map plotter using GPS co-ordinates Map multiple locations by address Very well-implemented free batch geocoder; accepts only address data, no GPS co-ordinates, but still excellent for some applications iPhone: Custom font loading Implementing Your Own Cocoa Bindings [...]]]></description>
			<content:encoded><![CDATA[<p>Links for October 25th through January 23rd:</p>

<ul class="delicious-bookmarks">
<li><a href="http://www.gpsvisualizer.com/map_input">GPS Visualizer: Draw a map from a GPS data file</a> Batch map plotter using GPS co-ordinates</li>
<li><a href="http://www.batchgeocode.com/">Map multiple locations by address</a> Very well-implemented free batch geocoder; accepts only address data, no GPS co-ordinates, but still excellent for some applications</li>
<li><a href="http://uihacker.blogspot.com/2009/01/iphone-custom-font-loading-complete.html">iPhone: Custom font loading</a> </li>
<li><a href="http://www.tomdalling.com/cocoa/implementing-your-own-cocoa-bindings">Implementing Your Own Cocoa Bindings &laquo;  Tom Dalling</a> </li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1684" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-october-25th-january-23rd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error -12986 and you</title>
		<link>http://atastypixel.com/blog/error-12986-and-you/</link>
		<comments>http://atastypixel.com/blog/error-12986-and-you/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 20:59:39 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Audio]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2010/01/10/error-12986-and-you/</guid>
		<description><![CDATA[A customer recently got in touch with me with an odd problem &#8212; Talkie for iPhone was going silent on him. After a little diagnosis, it turned out the audio system was throwing up an undocumented error &#8212; -12986 &#8212; while starting up on his iPod Touch. Some googling turned up this thread on Stack [...]]]></description>
			<content:encoded><![CDATA[<p>A customer recently got in touch with me with an odd problem &#8212; Talkie for iPhone was going silent on him.  After a little diagnosis, it turned out the audio system was throwing up an undocumented error &#8212; -12986 &#8212; while starting up on his iPod Touch.</p>

<p>Some googling turned up <a href="http://stackoverflow.com/questions/1540078/iphone-sdk-audio-session-error-12986-after-upgrade-to-3-1">this thread</a> on Stack Overflow, which presented a solution for the <code>AVAudioSession</code> framework: It turns out, on recent SDKs (somewhere equal to or above 3.0), the audio system will refuse to start up if the session is set to a recording mode, with no input device available, spitting out this error.  Alas, the error isn&#8217;t listed anywhere that I could find.</p>

<p>So, the way to fix it is to check whether there&#8217;s an input device available, then choose an audio category accordingly:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">OSStatus status;
&nbsp;
UInt32 inputAvailable<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>;
UInt32 size <span style="color: #002200;">=</span> <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>inputAvailable<span style="color: #002200;">&#41;</span>;
AudioSessionGetProperty<span style="color: #002200;">&#40;</span>kAudioSessionProperty_AudioInputAvailable, 
                        <span style="color: #002200;">&amp;</span>size, 
                        <span style="color: #002200;">&amp;</span>inputAvailable<span style="color: #002200;">&#41;</span>;
UInt32 sessionCategory;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> inputAvailable <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Set the audio session category for simultaneous play and record</span>
    sessionCategory <span style="color: #002200;">=</span> kAudioSessionCategory_PlayAndRecord;
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Just playback</span>
    sessionCategory <span style="color: #002200;">=</span> kAudioSessionCategory_MediaPlayback;
<span style="color: #002200;">&#125;</span>
&nbsp;
status <span style="color: #002200;">=</span> AudioSessionSetProperty <span style="color: #002200;">&#40;</span>kAudioSessionProperty_AudioCategory,
                                  <span style="color: #a61390;">sizeof</span> <span style="color: #002200;">&#40;</span>sessionCategory<span style="color: #002200;">&#41;</span>,
                                  <span style="color: #002200;">&amp;</span>sessionCategory<span style="color: #002200;">&#41;</span>;    
checkStatus<span style="color: #002200;">&#40;</span>status<span style="color: #002200;">&#41;</span>;</pre></div></div>


<p>It&#8217;s probably a good idea to respond when input becomes available or goes away, so add a property listener too:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> inputAvailablePropertyListener <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span>                      <span style="color: #002200;">*</span>inClientData,
                                     AudioSessionPropertyID    inID,
                                     UInt32                    inDataSize,
                                     <span style="color: #a61390;">const</span> <span style="color: #a61390;">void</span>                <span style="color: #002200;">*</span>inData<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> inID <span style="color: #002200;">=</span> kAudioSessionProperty_AudioInputAvailable <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        UInt32 <span style="color: #002200;">*</span>inputAvailable <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UInt32<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>inData;
        UInt32 sessionCategory;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">*</span>inputAvailable <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// Set the audio session category for simultaneous play and record</span>
            sessionCategory <span style="color: #002200;">=</span> kAudioSessionCategory_PlayAndRecord;
        <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// Just playback</span>
            sessionCategory <span style="color: #002200;">=</span> kAudioSessionCategory_MediaPlayback;
        <span style="color: #002200;">&#125;</span>
&nbsp;
        OSStatus status <span style="color: #002200;">=</span> AudioSessionSetProperty <span style="color: #002200;">&#40;</span>kAudioSessionProperty_AudioCategory,
                                                   <span style="color: #a61390;">sizeof</span> <span style="color: #002200;">&#40;</span>sessionCategory<span style="color: #002200;">&#41;</span>,
                                                   <span style="color: #002200;">&amp;</span>sessionCategory<span style="color: #002200;">&#41;</span>;    
        checkStatus<span style="color: #002200;">&#40;</span>status<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>then:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Listen for audio input availability</span>
status <span style="color: #002200;">=</span> AudioSessionAddPropertyListener<span style="color: #002200;">&#40;</span>kAudioSessionProperty_AudioInputAvailable,
                                         inputAvailablePropertyListener, 
                                         <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1786" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/error-12986-and-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Links for June 3rd through June 18th</title>
		<link>http://atastypixel.com/blog/links-june-3rd-june-18th/</link>
		<comments>http://atastypixel.com/blog/links-june-3rd-june-18th/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 00:00:26 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1631</guid>
		<description><![CDATA[Links for June 3rd through June 18th: Ten logo design tips from the field &#124; Logo Design Love 10 astonishing CSS hacks and techniques Adaptive CSS-Layouts: New Era In Fluid Layouts? effective techniques to create 100%-functional adaptive CSS-layouts WordPress Theme Development Frameworks If you build and develop WordPress themes often, you will probably be fed [...]]]></description>
			<content:encoded><![CDATA[<p>Links for June 3rd through June 18th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://www.logodesignlove.com/logo-design-tips">Ten logo design tips from the field | Logo Design Love</a> </li>
<li><a href="http://www.catswhocode.com/blog/10-astonishing-css-hacks-and-techniques">10 astonishing CSS hacks and techniques</a> </li>
<li><a href="http://www.smashingmagazine.com/2009/06/09/smart-fixes-for-fluid-layouts/">Adaptive CSS-Layouts: New Era In Fluid Layouts?</a> effective techniques to create 100%-functional adaptive CSS-layouts</li>
<li><a href="http://www.smashingmagazine.com/2009/05/27/wordpress-theme-development-frameworks/">WordPress Theme Development Frameworks</a> If you build and develop WordPress themes often, you will probably be fed up of all the repetitive code writing, the constantly checking of your mark-up and all you really want to do is focus on the design and the project-specific features. The answer is a WordPress development framework</li>
<li><a href="http://steveweller.com/articles/toolbar-icons/">How to Draw Pixel-Perfect iPhone Toolbar Icons</a> Using OmniGraffle</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1631" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-june-3rd-june-18th/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Links for March 17th through April 24th</title>
		<link>http://atastypixel.com/blog/links-march-17th-april-24th/</link>
		<comments>http://atastypixel.com/blog/links-march-17th-april-24th/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 00:00:41 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1514</guid>
		<description><![CDATA[Links for March 17th through April 24th: XSD Schema XML Validator A XSD Schema validator in Java, provides lots of useful information; use it with a schema and an xml instance Three20 Three20 is a collection of iPhone UI classes, like a photo viewer, and general utilities, like an HTTP disk cache. Three20 is derived [...]]]></description>
			<content:encoded><![CDATA[<p>Links for March 17th through April 24th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://www.herongyang.com/XML-Schema/JAXP-XSD-Schema-XML-Validator-Final-Version.html">XSD Schema XML Validator</a> A XSD Schema validator in Java, provides lots of useful information; use it with a schema and an xml instance</li>
<li><a href="http://github.com/joehewitt/three20/tree/master">Three20</a> Three20 is a collection of iPhone UI classes, like a photo viewer, and general utilities, like an HTTP disk cache. Three20 is derived from the Facebook iPhone app, which is one of the most downloaded iPhone apps ever.</li>
<li><a href="http://www.webdesignerwall.com/tutorials/css-decorative-gallery/">CSS Decorative Gallery</a> &#8230;How to decorate your images and photo galleries without editing the source images. The trick is very simple. All you need is an extra &lt;span&gt; tag and apply a background image to create the overlaying effect.</li>
<li><a href="http://en.wikipedia.org/wiki/Comet_(programming)">Comet (programming) &#8211; Wikipedia, the free encyclopedia</a> Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it</li>
<li><a href="http://www.cimgf.com/2008/03/26/cocoa-tutorial-awakefromnib-vs-applicationdidfinishlaunching/">Cocoa Is My Girlfriend &raquo; Cocoa Tutorial: awakeFromNib vs applicationDidFinishLaunching</a> A very good overview of the &#39;startup&#39; procedure for objects stored in IB nib/xibs.</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1514" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-march-17th-april-24th/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Podcast interview with Dan Grigsby of Mobile Orchard on Loopy&#8217;s development</title>
		<link>http://atastypixel.com/blog/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/</link>
		<comments>http://atastypixel.com/blog/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 08:49:26 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Loopy]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2009/03/03/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/</guid>
		<description><![CDATA[Last Thursday I did an interview with Dan Grigsby from Mobile Orchard; the interview is now online. Highlights from this interview include: From UIView to OpenGL: the seven different implementations it took to finalize its unique — and Best App Ever award-nominated — UI. From audio-queues to Remote IO: the four different architectural approaches he [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://atastypixel.com/blog/wp-content/uploads/2009/03/iphone-loopy-mobileorchard.png" width="106" height="200" alt="iphone-loopy-mobileorchard.png" title="iphone-loopy-mobileorchard.png" class="alignright noframe" />Last Thursday I did an interview with Dan Grigsby from <a href="http://www.mobileorchard.com">Mobile Orchard</a>; the <a href="http://www.mobileorchard.com/interview-with-michael-tyson-creator-of-the-audio-mixing-app-loopy/">interview</a> is now online.</p>

<blockquote>
<em>Highlights from this interview include:

From UIView to OpenGL: the seven different implementations it took to finalize its unique — and Best App Ever award-nominated — UI.

From audio-queues to Remote IO: the four different architectural approaches he tried before finalizing audio subsystems.

The travails of trying to implement echo cancelation.

The business of making a living off of a $10 app</em>
</blockquote>

<p>Listen to it <a href="http://www.mobileorchard.com/interview-with-michael-tyson-creator-of-the-audio-mixing-app-loopy/">here</a>, or <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=294369513">subscribe in iTunes</a> (30 min.)</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1462" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Understanding error codes</title>
		<link>http://atastypixel.com/blog/understanding-error-codes/</link>
		<comments>http://atastypixel.com/blog/understanding-error-codes/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 12:55:52 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2009/02/28/understanding-error-codes/</guid>
		<description><![CDATA[Just in case there&#8217;s someone else that didn&#8217;t know this, when one gets an error code from one of the iPhone/OS X SDKs with no other information available, it can usually be looked up in the MacErrors.h header. Just open a terminal, type: open -h MacErrors.h Then do a search for your error code and [...]]]></description>
			<content:encoded><![CDATA[<p>Just in case there&#8217;s someone else that didn&#8217;t know this, when one gets an error code from one of the iPhone/OS X SDKs with no other information available, it can usually be looked up in the <i>MacErrors.h</i> header.  Just open a terminal, type:</p>

<p><tt>open -h MacErrors.h</tt></p>

<p>Then do a search for your error code and you&#8217;ll hopefully find a corresponding macro name that gives some indication of what went wrong.</p>

<p>Failing that, if you have an inkling of where the error occurred (eg. the AudioToolbox framework), then you can often find the error defined within the framework&#8217;s headers:</p>

<div style="overflow: auto; white-space: nowrap; font-size: 0.7em"><tt>$ grep -r '10863' /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/System/Library/Frameworks/AudioToolbox.framework/
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/System/Library/Frameworks/AudioToolbox.framework/Headers/AUGraph.h:   kAUGraphErr_CannotDoInCurrentContext    = -10863,</tt></div>

<p>That was, type in Terminal <tt>grep -r '<i>the error code</i>'</tt>, then drag the framework straight from XCode into the Terminal, where the path will be inserted.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1453" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/understanding-error-codes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Links for February 10th through February 27th</title>
		<link>http://atastypixel.com/blog/links-february-10th-february-27th/</link>
		<comments>http://atastypixel.com/blog/links-february-10th-february-27th/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 02:02:42 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1383</guid>
		<description><![CDATA[Links for February 10th through February 27th: TinEye Reverse Image Search TinEye is a reverse image search engine. You can submit an image to TinEye to find out where it came from, how it is being used, if modified versions of the image exist, or to find higher resolution versions. Traffic Shaping in Mac OS [...]]]></description>
			<content:encoded><![CDATA[<p>Links for February 10th through February 27th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://tineye.com/">TinEye Reverse Image Search</a> TinEye is a reverse image search engine. You can submit an image to TinEye to find out where it came from, how it is being used, if modified versions of the image exist, or to find higher resolution versions.</li>
<li><a href="http://www.macgeekery.com/hacks/software/traffic_shaping_in_mac_os_x">Traffic Shaping in Mac OS X | Mac Geekery</a> &quot;&#8230;Create several pipes that have a set bandwidth and other properties for all packets that get filed into them; you then add queues to those pipes that determine what priority certain requests will get in that pipe; then you add actual firewall rules to identify packets and file them into queues.&quot;</li>
<li><a href="http://www.brandonwalkin.com/blog/2008/11/13/introducing-bwtoolkit/">Brandon Walkin  &raquo; Introducing BWToolkit</a> BWToolkit is a BSD licensed plugin for Interface Builder 3 that contains commonly used UI elements and other useful objects. Using these objects is as simple as dragging them from the library to your canvas or document window. In particular, &quot;No Code&quot; preferences window and tabbed sheets.</li>
<li><a href="http://forums.mactalk.com.au/56/55433-aussie-iphone-app-developers-irs-2.html">Aussie iPhone app developers and the IRS?</a> Discussion about tax details for Australian iPhone developers. It appears the advice from Apple on the tax form is incorrect for sales on the App Store.</li>
<li><a href="http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting">google-toolbox-for-mac &#8211; How to do iPhone unit testing</a> This is a quick tutorial on doing iPhone unit testing using the facilities in the Google Toolbox For Mac</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1383" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-february-10th-february-27th/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Happy Birthday, Loopy (Thanks, Apple)</title>
		<link>http://atastypixel.com/blog/happy-birthday-loopy-thanks-apple/</link>
		<comments>http://atastypixel.com/blog/happy-birthday-loopy-thanks-apple/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 00:37:05 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Career]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Lifestyle]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2009/02/17/happy-birthday-loopy-thanks-apple/</guid>
		<description><![CDATA[Loopy is 2 months old! More interestingly, it&#8217;s just passed the $4000 AUD ($2640 USD, or $2050 EUR &#8211; stupid Australian dollar!) earnings point, which works out at about $500 a week. For essentially a niche app, I&#8217;m pretty damn happy. I can&#8217;t think of any other marketplace in history where almost anyone can jump [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://atastypixel.com/blog/wp-content/uploads/2009/02/loopy-2months.png" width="130" height="200" alt="loopy-2months.png" title="loopy-2months.png" class="alignright noframe" />Loopy is 2 months old!</p>

<p>More interestingly, it&#8217;s just passed the $4000 AUD (<em>$2640 USD, or $2050 EUR &#8211; stupid Australian dollar!</em>) earnings point, which works out at about $500 a week.  For essentially a niche app, I&#8217;m pretty damn happy.</p>

<p>I can&#8217;t think of any other marketplace in history where almost anyone can jump in with an idea and a little ability, and come out with something resembling a salary almost immediately.  I can&#8217;t imagine the indie lifestyle has ever been more attainable.</p>

<p>Apple have done something amazing here, in creating a marketplace fully equipped with their mature infrastructure, their brand, and most importantly, their enormous user base.  They&#8217;re basically giving developers the fruits of all the work they&#8217;ve done over the past decades in building Apple&#8217;s brand and customer base (in return for 30% of sales, of course!).</p>

<p>Now that&#8217;s some profitable symbiosis.  Apple &#8211; thank you.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1433" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/happy-birthday-loopy-thanks-apple/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
