<?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>Dr. Touch &#187; Q&amp;A</title>
	<atom:link href="http://www.drobnik.com/touch/category/qa/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.drobnik.com/touch</link>
	<description>Bitching and Stiching iPhone Apps (almost) since 1974</description>
	<lastBuildDate>Thu, 02 Sep 2010 13:05:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to become GREAT at iOS Development</title>
		<link>http://www.drobnik.com/touch/2010/07/how-to-become-great-at-ios-development/</link>
		<comments>http://www.drobnik.com/touch/2010/07/how-to-become-great-at-ios-development/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 07:05:46 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=2848</guid>
		<description><![CDATA[I'm interested in getting your questions because answer [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m interested in getting your questions because answering them helps me structure the material in my head. And there&#8217;s a saying that &#8220;what you teach you learn&#8221;, because of that.</p>
<p><a href="http://itunes.com/apps/devinsnipes">Devin Snipes</a>, an aspiring young iOS Developer asks:</p>
<blockquote><p>Hello Dr. Touch,</p>
<p>My name is Devin Snipes, I&#8217;m 15 years old and I&#8217;m an iPhone Developer. I&#8217;ve been following your work for a little less than a year, and I&#8217;ve grown to love it. Your work is amazing, and I hope to someday be as good as you are in programming for the iOS platform. I currently have a few iPhone applications on the AppStore, but nothing compared to yours.</p>
<p>I&#8217;d like to ask you a few questions that will hopefully give me more insight on your developmental skills and how I can improve on my skill.</p></blockquote>
<p>Well, &#8220;You catch more flies with honey than vinegar&#8221;. If somebody asks so nicely I&#8217;ll usually try to respond with something useful.</p>
<blockquote><p>1. How did you become so great at programming for the iPhone?</p></blockquote>
<p>I&#8217;m doing it full time only since last December. And before that I was looking at code on most days for a couple of hours. Do you know the rule of 10,000? It says that if you want to be world-class in any field you have to invest 10,000 hours in total. Before I got into developing for the iPhone I was collecting programming time for many years. So I probably reached 10,000 a while ago. But that&#8217;s not strictly Cocoa time. At 10 hours a day it takes you around 3 years to reach 10,000. So I&#8217;m probably around 5,000 hours doing iPhone stuff.</p>
<p><span id="more-2848"></span></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>That makes me an intermediary iOS Developer with good analytics and good abstraction skills. Not yet a real expert, but (working on) getting there. There are people who are more &#8220;expert&#8221; than myself, the stars of Cocoa, if you will. Check out how their bylines describe the place that iOS development has taken in their life.</p>
<ul>
<li><a href="http://mattgemmell.com/">Matt Legend Gammell</a> (&#8220;Instinctive Code&#8221;)</li>
<li><a href="http://www.cimgf.com/">Marcus Zarra</a> (&#8220;Cocoa is my Girlfriend&#8221;)</li>
<li><a href="http://cocoawithlove.com">Matt Gallagher</a> (&#8220;Cocoa with Love&#8221;)</li>
<li><a href="http://www.bignerdranch.com/instructors/hillegass.shtml">Aaron Hillegass</a> (&#8220;Big Nerd Ranch&#8221;)</li>
</ul>
<p>My trick is to vary the time I can log towards this goal. I&#8217;m not only programming, I&#8217;m also watching training videos, doing coaching, teaching development courses, troubleshooting in other people&#8217;s apps, making encapsulated software components that I&#8217;m selling, I have some apps of my own and lots of contract work. The latter in itself is only partially coding because you also have to train your brain in how to put people&#8217;s ideas and specifications into code.</p>
<p>If you program hours and hours on end then you also begin to come up with ways how to use your time more effectively. Like for example putting together a set of tools and utility classes that you are using all the time to quickly achieve standard tasks. Take it one level higher and then you are writing <a href="http://www.drobnik.com/touch/parts-store/">software components</a> that can be plugged into your own apps or even other people&#8217;s apps. Writing reusable code not only makes your life easier, it also forces you to think through and abstract interfaces such that your components are loosely coupled. That&#8217;s the essence of MVC (Model-View-Controller), the heart of Cocoa.</p>
<p>And some other activities that are only peripheral like blogging and podcasting. But I find that as long as you center it on a single topic then all your activities cross-pollinate. That&#8217;s why I gave up on Windows support or programming for any other platform than iOS. And I find that by writing about programming it forces me to chew through boring documentation and try out the techniques I&#8217;m covering. This META level of Cocoa Coding teaches me just as well as my readers.</p>
<p>Obviously it helps if you can do it full-time, that&#8217;s why I detailed this multi-prong strategy in my previous blog article <a href="http://www.drobnik.com/touch/2010/01/business-as-unusual/">Business as Unusual</a>.</p>
<blockquote><p>2. Who made the shirt you were wearing in your <a href="http://www.youtube.com/watch?v=C5m9SnTxbo0">iPhone 4 unboxing video</a>?</p></blockquote>
<p>It was given to me last Christmas by my brother-in-law who has a friend who has the tools to make T-Shirts. Do you think I should start a line of Dr. Touch merchandising items? <img src='http://www.drobnik.com/touch/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<blockquote><p>3. This might be going a little over-board, but do you mind taking a look at a few of my current iPhone &amp; iPad applications and tell me how I could improve them? I&#8217;d really appreciate it if you took the time to look over them and give me your diagnosis on how they can be improved or fixed up.</p></blockquote>
<p>I&#8217;m only judging what I see on the app store and those are just my first gut responses. If you have a minute to look at these apps too then please add your feedback in the comments.</p>
<p><a href="http://itunes.apple.com/at/app/uchat-for-iphone/id366365571?mt=8">uChat for iPhone</a> (FREE) / <a href="http://itunes.apple.com/at/app/uchat-for-ipad/id372118225?mt=8">uChat for iPad</a> ($0.99)</p>
<ul>
<li>make this into a hybrid app, drop the price to free and instead add iAds.</li>
<li>don&#8217;t have any screens that cover the entire display and only have 4 buttons</li>
<li>it looks to me that the chat screen only use half of the available area</li>
<li>Ramp up the design, now there is next to none, using the gray scroll view background makes the app look dead</li>
</ul>
<p><a href="http://itunes.apple.com/at/app/am-i-ugly/id358913358?mt=8">Am I Ugly</a> (FREE)</p>
<ul>
<li>there is no design, so the hard answer: Yes, you (the app) are ugly</li>
<li>apart from this it&#8217;s ok for a &#8220;Hello App Store&#8221; app, but to be something that you would want people to enjoy you&#8217;d have to make it way more useful.</li>
</ul>
<p><a href="http://itunes.apple.com/at/app/itweet/id357632437?mt=8">iTweet!</a> (FREE)</p>
<ul>
<li>there are half-baked attempts at design, but I don&#8217;t think you should mix gray round UI elements with standard blue nav bars</li>
<li>navigation controllers should always have a title for the nav bars to prevent user confusion</li>
<li>with iOS4 the need for the core functionality (tweet and listen to music simultaneously) is in doubt</li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p><strong>Summary</strong></p>
<p>By the number of apps alone you are way ahead of me. It took me half a year part time work to get to 4 and I was 34 at that time. You have that number (and the attached experience) at half my age. So I won&#8217;t be surprised if you overtake me sooner or later.</p>
<p>You seem to be making good progress since I see you using not just clicked-together standard UI stuff, but you&#8217;re also using GameKit, interfacing Twitter API, TwitPi and other advanced techniques. This tells me that you have grown sufficient abstractive reasoning that you can take an API or framework and use it from your code.</p>
<p>Apple says that 60% of the work that flows into an app should be design and your apps are lacking in this regard. So either you should work on your app design skills by trying to paper-prototype apps similar to famous examples like Tapbots. Or you should hire a friend who has &#8220;an eye for such things&#8221; to help you lay out the UX (User Experience) and UI (User Interface).</p>
<p>Apart from this I can only recommend a daily work out regime. Collect Cocoa hours, 10k or more. Maybe that&#8217;s an idea for an app&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2010/07/how-to-become-great-at-ios-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing on UIImages</title>
		<link>http://www.drobnik.com/touch/2010/07/drawing-on-uiimages/</link>
		<comments>http://www.drobnik.com/touch/2010/07/drawing-on-uiimages/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 15:49:46 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=2774</guid>
		<description><![CDATA[Jayesh asks:
Thanks for your article on UIImage from U [...]]]></description>
			<content:encoded><![CDATA[<p>Jayesh asks:</p>
<blockquote><p>Thanks for your article on UIImage from UIView.</p>
<p>Need one more favor; I am in situation where I want to draw line or area (e.g. rectangle) on UI Image (e.g. Blue print) and save it again.</p>
<p>Basically I will show image and put circle / rectangle on image showing area in blue print and save it.</p>
<p>How should I do that? Can you please suggest approach, sample codes etc?</p></blockquote>
<p>So the task is to take a UIImage, make it writable in some way and then make a new image out of that for later use. Off the top of my head, I can immediately think of two ways to do that: with UIKit and with CoreGraphics. CoreGraphics has a slight advantage, being lower level, of being thread-safe. But for a simple graphical addition to an existing image I see nothing wrong with UIKit. As usual you should only do very quick operations with UIKit because it requires to be run on the main thread which is the only thread updating the user interface.</p>
<p><span id="more-2774"></span><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>So, first, let&#8217;s create a method that adds a circle to a passed UIImage.</p>

<div class="wp_codebox"><table><tr id="p27741"><td class="code" id="p2774code1"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageByDrawingCircleOnImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// begin a graphics context of sufficient size</span>
	UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>image.size<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// draw original image into the context</span>
	<span style="color: #002200;">&#91;</span>image drawAtPoint<span style="color: #002200;">:</span>CGPointZero<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get the context for CoreGraphics</span>
	CGContextRef ctx <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// set stroking color and draw circle</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor redColor<span style="color: #002200;">&#93;</span> setStroke<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// make circle rect 5 px from border</span>
	CGRect circleRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>,
				image.size.width,
				image.size.height<span style="color: #002200;">&#41;</span>;
	circleRect <span style="color: #002200;">=</span> CGRectInset<span style="color: #002200;">&#40;</span>circleRect, <span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// draw circle</span>
	CGContextStrokeEllipseInRect<span style="color: #002200;">&#40;</span>ctx, circleRect<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// make image out of bitmap context</span>
	UIImage <span style="color: #002200;">*</span>retImage <span style="color: #002200;">=</span> UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// free the context</span>
	UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> retImage;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This code is only using UIKit methods, except for the lonely CGContextStrokeEllipseInRect. This is the reason why we need to retrieve a handle for the current context. UIKit has it&#8217;s own reference so that all UI* methods work without specifically setting a context. Should you ever need to force a specific context that you had created elsewhere, then you can use UIGraphicsPushContext to put this other context &#8220;topmost&#8221; and UIGraphicsPopContext to clean up.</p>
<p>To use the above mentioned function all you need is this:</p>

<div class="wp_codebox"><table><tr id="p27742"><td class="code" id="p2774code2"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// load image from bundle</span>
UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;user-1.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// call circle drawing</span>
UIImage <span style="color: #002200;">*</span>imageWithCircle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self imageByDrawingCircleOnImage<span style="color: #002200;">:</span>image<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// save it to documents</span>
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>documentsPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory,
							NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span> lastObject<span style="color: #002200;">&#93;</span>;
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>filePath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>documentsPath stringByAppendingPathComponent<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;output.png&quot;</span><span style="color: #002200;">&#93;</span>;
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> <span style="color: #002200;">*</span>imageData <span style="color: #002200;">=</span> UIImagePNGRepresentation<span style="color: #002200;">&#40;</span>imageWithCircle<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>imageData writeToFile<span style="color: #002200;">:</span>filePath atomically<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Saved new image to %@&quot;</span>, filePath<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>So, that&#8217;s the UIKit variant. Sometimes though you need to draw on a background thread and thus have to be sure to use thread-safe methods.</p>
<p>The <a href="http://developer.apple.com/iphone/library/releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS4.html">iOS 4 release notes</a> mention that drawing images and fonts is thread-safe as of SDK 4.0.</p>
<blockquote><p>Drawing to a graphics context in UIKit is now thread-safe. Specifically:</p>
<ul>
<li>The routines used to access and manipulate the graphics context can now correctly handle contexts residing on different threads.</li>
<li>String and image drawing is now thread-safe.</li>
</ul>
<p>Using color and font objects in multiple threads is now safe to do.</p></blockquote>
<p>So the above sample should be fine for most purposes. Calling it on a background thread would work on 4.0 and above, but cause weird error messages on earlier SDK versions. One example might be if you needed to draw contents of a CATiledLayer, which is occuring on a special background thread.</p>
<p>So how would this example look in CoreGraphics and without UIKit functions?</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>First we would require a method to create a bitmap context of sufficient size. I took an example I found somewhere and modified it to my needs. I actually discovered that you don&#8217;t have to allocate the storage for the bitmap context yourself, it works fine as of 3.2, even though the documentation states that you should not do that before 4.0. Well, it works, I&#8217;ll stick with it.</p>

<div class="wp_codebox"><table><tr id="p27743"><td class="code" id="p2774code3"><pre class="objc" style="font-family:monospace;">CGContextRef newBitmapContextSuitableForSize<span style="color: #002200;">&#40;</span>CGSize size<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> pixelsWide <span style="color: #002200;">=</span> size.width;
	<span style="color: #a61390;">int</span> pixelsHigh <span style="color: #002200;">=</span> size.height;
    CGContextRef    context <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
    CGColorSpaceRef colorSpace;
   <span style="color: #11740a; font-style: italic;">// void *          bitmapData;</span>
    <span style="color: #a61390;">int</span>             bitmapByteCount;
    <span style="color: #a61390;">int</span>             bitmapBytesPerRow;
&nbsp;
    bitmapBytesPerRow   <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>pixelsWide <span style="color: #002200;">*</span> <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">//4</span>
    bitmapByteCount     <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>bitmapBytesPerRow <span style="color: #002200;">*</span> pixelsHigh<span style="color: #002200;">&#41;</span>;
&nbsp;
   <span style="color: #11740a; font-style: italic;">/* bitmapData = malloc( bitmapByteCount );
&nbsp;
	memset(bitmapData, 0, bitmapByteCount);  // set memory to black, alpha 0
&nbsp;
    if (bitmapData == NULL)
    {
        return NULL;
    }
*/</span>
	colorSpace <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceRGB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;  	
&nbsp;
	context <span style="color: #002200;">=</span> CGBitmapContextCreate <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">NULL</span>, <span style="color: #11740a; font-style: italic;">// instead of bitmapData</span>
					pixelsWide,
					pixelsHigh,
					<span style="color: #2400d9;">8</span>,      <span style="color: #11740a; font-style: italic;">// bits per component</span>
					bitmapBytesPerRow,
					colorSpace,
					kCGImageAlphaPremultipliedFirst<span style="color: #002200;">&#41;</span>;
	CGColorSpaceRelease<span style="color: #002200;">&#40;</span> colorSpace <span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>context<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: #11740a; font-style: italic;">// free (bitmapData);</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">NULL</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> context;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Note that if you would put the malloc back in then you also need to free the memory pointer after you&#8217;re done. Previously I did not know that so all this memory leaked. This approach of passing NULL instead of a pointer apparently works on 3.2 and above, officially from 4.0.</p>

<div class="wp_codebox"><table><tr id="p27744"><td class="code" id="p2774code4"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageByDrawingCircleOnImageCG<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// begin a graphics context of sufficient size</span>
	CGContextRef ctx <span style="color: #002200;">=</span> newBitmapContextSuitableForSize<span style="color: #002200;">&#40;</span>image.size<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// need to flip the transform matrix</span>
	<span style="color: #11740a; font-style: italic;">// CoreGraphics has (0,0) in lower left</span>
	CGContextScaleCTM<span style="color: #002200;">&#40;</span>ctx, <span style="color: #2400d9;">1</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
	CGContextTranslateCTM<span style="color: #002200;">&#40;</span>ctx, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">-</span>image.size.height<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// draw original image into the context</span>
	CGRect imageRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, image.size.width, image.size.height<span style="color: #002200;">&#41;</span>;
	CGContextDrawImage<span style="color: #002200;">&#40;</span>ctx, imageRect, image.CGImage<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// set stroking color and draw circle</span>
	CGContextSetRGBStrokeColor<span style="color: #002200;">&#40;</span>ctx, <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// make circle rect 5 px from border</span>
	CGRect circleRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>,
			image.size.width,
			image.size.height<span style="color: #002200;">&#41;</span>;
	circleRect <span style="color: #002200;">=</span> CGRectInset<span style="color: #002200;">&#40;</span>circleRect, <span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">5</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// draw circle</span>
	CGContextStrokeEllipseInRect<span style="color: #002200;">&#40;</span>ctx, circleRect<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// make image out of bitmap context</span>
	CGImageRef cgImage <span style="color: #002200;">=</span> CGBitmapContextCreateImage<span style="color: #002200;">&#40;</span>ctx<span style="color: #002200;">&#41;</span>;
	UIImage <span style="color: #002200;">*</span>retImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span>cgImage<span style="color: #002200;">&#93;</span>;
	CGImageRelease<span style="color: #002200;">&#40;</span>cgImage<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// free the context</span>
	CGContextRelease<span style="color: #002200;">&#40;</span>ctx<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> retImage;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Besides of having to use this helper function to create a suitable bitmap context you should also note that we have to go via creating a CGImage first. From this we can then create a UIImage. Also you might have spotted that we need to turn the transformation matrix upside down because otherwise all our graphics would be that. CoreGraphics generally assumes the origin of drawing in the lower left hand corner.</p>
<p>I&#8217;ve shown you both methods of drawing on images, now you can take your pick which one you prefer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2010/07/drawing-on-uiimages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Service Announcement: Upgrading iOS 4 GM</title>
		<link>http://www.drobnik.com/touch/2010/06/service-announcement-upgrading-ios-4-gm/</link>
		<comments>http://www.drobnik.com/touch/2010/06/service-announcement-upgrading-ios-4-gm/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 15:16:54 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=2749</guid>
		<description><![CDATA[A not-to-be-named friend asked me:
I would like your o [...]]]></description>
			<content:encoded><![CDATA[<p>A not-to-be-named friend asked me:</p>
<blockquote><p>I would like your opinion the GM build. Should I (not sure how) remove it and then load the new iOS 4.0? I know it wouldn&#8217;t have the game center, but I don&#8217;t use that anyway. I am afraid if I don&#8217;t remove, the software will expire (happened on beta 4 and then I couldn&#8217;t use the phone (or back up first) until I could load the latest.</p></blockquote>
<p>If you&#8217;re one of those developers that managed to install the iOS 4.0 Gold Master right after Uncle Steve announced it, then you might wonder if you should update to the really final release that came out just now.</p>
<p><em>Wait! That&#8217;s a trick question&#8230;!</em></p>
<p><span id="more-2749"></span><br />
<script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>A version is called <a href="http://en.wikipedia.org/wiki/Golden_master">Gold Master</a> if it is the final code that would be put on a master CD to be mass-produced by the factory. Since we are getting iOS updates via the Internet there is no longer any physical media involved. A different name, less anachronistic, would have been RTM (&#8220;Release to Manufacturing&#8221;). Or not, there is no manufacturing involved in unleashing Apple software upon the world.</p>
<p>So let&#8217;s see if that really WAS true and we got the really REALLY finaly 4.0 version after the Stevenote. To check on which version is the latest I used a trick I learned from TUAW to get a list of the ipsw restore files bearing version 4.0. They employed the OSX built-in text-to-speech engine to have the Mac <a href="http://www.tuaw.com/2010/06/21/ios-4-0-firmware-release-expected-momentarily-quick-terminal-ti/">tell you as soon as the 4.0 version became available</a> on Apple&#8217;s servers. A cool trick! But we need only the first line of their script:</p>

<div class="wp_codebox"><table><tr id="p27495"><td class="code" id="p2749code5"><pre class="sh" style="font-family:monospace;">curl -s -L http://phobos.apple.com/version | grep -i Restore | grep -i iPhone | grep -i 4.0</pre></td></tr></table></div>

<p>This part gets an XML file from Apple&#8217;s servers and cuts out the lines that include iPhone, Restore and 4.0. You get several lines that all share the same build number: <strong>8A293</strong>.</p>
<p>Now let&#8217;s cross check that against our own phone, that we have already be-goldened.</p>
<p><a href="http://www.drobnik.com/touch/wp-content/uploads/2010/06/IMG_0002.png"><img class="alignnone size-full wp-image-2750" title="Gold Master system info" src="http://www.drobnik.com/touch/wp-content/uploads/2010/06/IMG_0002.png" alt="" width="320" height="480" /></a></p>
<p>Down there at Version it also says 8A293. Heureka! Apple told the truth when they told us that we developers will get the final shipping version of iOS 4.0.</p>
<p>Check for yourself if you have this build number. If yes, then you might see if you can fool somebody else and ask them if they already upgraded to the really final 4.0. <img src='http://www.drobnik.com/touch/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2010/06/service-announcement-upgrading-ios-4-gm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Timing is Everything</title>
		<link>http://www.drobnik.com/touch/2010/03/timing-is-everything/</link>
		<comments>http://www.drobnik.com/touch/2010/03/timing-is-everything/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 08:03:02 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=2295</guid>
		<description><![CDATA[zeke817 asks:
Hey guys just wondering how to put a tim [...]]]></description>
			<content:encoded><![CDATA[<p>zeke817 asks:</p>
<blockquote><p>Hey guys just wondering how to put a timer in the appdelegate. I need a timer to keep playing on through multiple views instead of just playing on 1 view. Any help apperciated</p></blockquote>
<p>Using timers is pretty simple. There are plenty examples around and it&#8217;s not difficult to understand. Having said that, I am responding to this question for three reasons:</p>
<ol>
<li>my posts on my blog have been pretty scarce recently due to lots of programming for customer projects</li>
<li>I think I should at least document how I am using timers so that I can refer people to this post when the question arises again and again.</li>
<li>Explaining a simple thing to somebody else is the best way to train clarity in teaching.</li>
</ol>
<p>Generally speaking timers are not instantiated but <em>scheduled</em>. The difference is that the SDK/OS takes care of their memory management and we only have to worry about whether or not we want them to fire. So we don&#8217;t need to ever release a timer, instead we <em>invalidate</em> it.</p>
<p><span id="more-2295"></span><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>Now your choice is to have a timer fire just once or repeatedly. If you want it ongoing then it&#8217;s wise to have an instance variable defined in the header for saving the address of the NSTimer instance in so that we can command it to invalidate itself once we no longer want it to go on. It it&#8217;s just a one-off thing, then we don&#8217;t need an IVAR.</p>
<p>That means adding this to the header&#8217;s interface description between the curly brackets, a variable named &#8220;timer&#8221; which points (asterisk) to an NSTimer instance.</p>

<div class="wp_codebox"><table><tr id="p22956"><td class="code" id="p2295code6"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/"><span style="color: #400080;">NSTimer</span></a> <span style="color: #002200;">*</span>myTimer;</pre></td></tr></table></div>

<p>Somewhere in our code we want to start the timer. There are two methods of creating a timer, via invocation or target/selector. The second one is easier to understand so it&#8217;s the one I&#8217;ve been using. With it you specify the method that you want to be called back on every time the timer fires.</p>

<div class="wp_codebox"><table><tr id="p22957"><td class="code" id="p2295code7"><pre class="objc" style="font-family:monospace;">myTimer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/"><span style="color: #400080;">NSTimer</span></a> scheduledTimerWithTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">5.0</span> target<span style="color: #002200;">:</span>self
                               selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>timerFired<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>That creates a timer, that will fire after 5 seconds and then repeatedly every 5 seconds. When it fires it will call timerFired: in the same instance you scheduled it, due to using self. You could also be passing some user data, but we don&#8217;t need that for now, so we pass nil.</p>
<p>Of course we need to provide the timerFired: method as well:</p>

<div class="wp_codebox"><table><tr id="p22958"><td class="code" id="p2295code8"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> timerFired<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/"><span style="color: #400080;">NSTimer</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>timer
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tick&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>If you have an ongoing timer the least you should do is to invalidate it in the class dealloc. If you are invalidating it without destroying the instance you should also set your instance variable to nil because invalidating an already invalidated timer causes an exception. But [nil invalidate] is always ignored by objective-C.</p>

<div class="wp_codebox"><table><tr id="p22959"><td class="code" id="p2295code9"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>myTimer invalidate<span style="color: #002200;">&#93;</span>, myTimer <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #11740a; font-style: italic;">// other releases</span>
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Having a pointer to the timer allows you to do a couple of useful things with the instance methods that NSTimer provides. You can <em>fire</em> it manually, you can ask it if it <em>isValid</em> or you can <em>invalidate</em> it. Because internally NSTimer works with dates (more precisely: with a time duration in seconds since a reference date) for when it fires and not durations you can also get <em>fireDate</em> or <em>setFireDate</em>.</p>
<p>Now about userInfo, you can put anything in there that you like to have present as data in the timerFired method. If you&#8217;re just scheduling timer events for self, then this is mostly useless as you can access all instance variables anyway. But if you have some calculated data from right before the scheduling that&#8217;s not in an IVAR then you can pass it. Or if you are scheduling the receiver of the timerEvent to be a different object then it has much more usefulness.</p>
<p>Timers are a great invention, especially for achieving repeated calling of a given method. Though when you find yourself creating timers with repeats:NO then you might be better of using performSelector:withObject:afterDelay:, I think this is cleaner especially since another developer usually expects timers to be continuous.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2010/03/timing-is-everything/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>“Are You a Cocoa Crack?” Quiz (2)</title>
		<link>http://www.drobnik.com/touch/2010/02/are-you-a-cocoa-crack-quiz-2/</link>
		<comments>http://www.drobnik.com/touch/2010/02/are-you-a-cocoa-crack-quiz-2/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:14:35 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=1866</guid>
		<description><![CDATA[It's been more than half a year since I published the f [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been more than half a year since I published the <a href="http://www.drobnik.com/touch/2009/07/cocoa-crack-quiz-1/">first installment</a> of this Quiz aimed at Cocoa Cracks. Back then people seemed to like the challenges I presented. So I collected a few more nuggets. If you ever stumbled on an crash or strange result that you did not expect, then mail it to me.</p>
<p>These questions will show if you are really the Cocoa Crack you like to believe to be. To see the answer highlight the answer text with your mouse. No peeking! Respond in the comments how many you got correct.</p>
<p><strong>Quiz:</strong> Warm Up. Your UI designer gave you a PNG with the instruction &#8220;just tile this in the background&#8221; of a UIView. Do you have to subclass UIView, overriding the drawRect of the view and performing drawImage for all tiles? Or is there a simpler method?</p>
<p><strong>Answer:</strong> <span style="color: #fbfbfb;">You create a UIImage by loading the PNG and then create a &#8220;color&#8221; from it with [UIColor colorWithPatternImage:image]. Then you apply this as the background color of the view.<br />
</span></p>
<hr /><strong>Quiz:</strong> Suddenly your app crashes and when the debugger opens it first loads lots of stack frames. All you did is override a property like shown below. What&#8217;s the bug? Bonus Question: where&#8217;s the memory leak?</p>

<div class="wp_codebox"><table><tr id="p186610"><td class="code" id="p1866code10"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setTextColor<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIColor <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newTextColor
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>newTextColor <span style="color: #002200;">!=</span> textColor<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>textColor release<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>newTextColor<span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			self.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newTextColor retain<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// default</span>
		<span style="color: #002200;">&#123;</span>
			self.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		bubbleView.highlightedTextColor <span style="color: #002200;">=</span> textColor;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p><strong>Answer:</strong> <span style="color: #fbfbfb;">You have an endless recursion. self.textColor is a simplified way to write [self setTextColor:]. So this method keeps calling itself until the stack is full and your app gets terminated. The leak is calling retain on an assignment to a retaining property. But if you fix the crashing bug by removing both self then there is no leak.</span></p>
<hr /><strong>Quiz: </strong>You want to draw two lines in a drawRect, a horizontal black line and a white line 1 pixel below it. So you write the following code. But instead of the intended result you get this picture in magnification, the black is dark gray and the white is a lighter shade of gray. Why is that and how would you fix it?</p>
<p><a href="http://www.drobnik.com/touch/wp-content/uploads/2010/01/Picture-4.png"><img class="alignnone size-full wp-image-1868" title="Lines" src="http://www.drobnik.com/touch/wp-content/uploads/2010/01/Picture-4.png" alt="" width="205" height="8" /></a></p>

<div class="wp_codebox"><table><tr id="p186611"><td class="code" id="p1866code11"><pre class="objc" style="font-family:monospace;">CGContextRef currentContext <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Draw a black line at the top and a white line 1 pixel below</span>
&nbsp;
CGContextSetLineWidth<span style="color: #002200;">&#40;</span>currentContext, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
&nbsp;
CGContextSetRGBStrokeColor<span style="color: #002200;">&#40;</span>currentContext, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// Black</span>
CGContextMoveToPoint<span style="color: #002200;">&#40;</span>currentContext, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// top left</span>
CGContextAddLineToPoint<span style="color: #002200;">&#40;</span>currentContext, rect.size.width, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// to top right</span>
CGContextStrokePath<span style="color: #002200;">&#40;</span>currentContext<span style="color: #002200;">&#41;</span>;
&nbsp;
CGContextSetRGBStrokeColor<span style="color: #002200;">&#40;</span>currentContext, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// White</span>
CGContextMoveToPoint<span style="color: #002200;">&#40;</span>currentContext, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// left</span>
CGContextAddLineToPoint<span style="color: #002200;">&#40;</span>currentContext, rect.size.width, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
CGContextStrokePath<span style="color: #002200;">&#40;</span>currentContext<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Answer:</strong> <span style="color: #fbfbfb;">Core Graphics does not work on pixels directly, that&#8217;s why you use floating point numbers for coordinates. It works on coordinates and the resulting bitmap is always rendered. Half of the line width is always applied half a unit to each side of the line. To remedy this you need to move the coordinates of the lines by half a unit up or down.</span></p>
<p>(Thanks to <a href="http://www.sendmetospace.co.uk/">Michael Kaye</a> for sending this in)</p>
<hr /><strong>Quiz: </strong>You have a UITableView and would like for it to have rounded corners. What&#8217;s the fastest way to achieve that? Which header is necessary?</p>
<p><strong>Answer:</strong> <span style="color: #fbfbfb;">You can have any UIView have rounded corners by setting the cornerRadius property of it&#8217;s CALayer. The layer of a UIView is actually what&#8217;s responsible for drawing the contents of the view. You need: to import  &lt;QuartzCore/QuartzCore.h&gt;</span></p>
<hr /><strong>Quiz: </strong>You have created a UIViewController which shows a UIWebView and has a navigation bar. You want to set the title on the bar to be the same as the  title of the HTML document once loading has finished. Would you need to parse the HTML with NSScanner to find the &lt;title&gt; tag and extract it? Or is there a simpler method?</p>
<p><strong>Answer:</strong> <span style="color: #fbfbfb;">In the webViewDidFinishLoad: delegate method you retrieve the title via javascript and set it like that. self.title = [webView stringByEvaluatingJavaScriptFromString: @"document.title"];<br />
</span></p>
<hr />
<p><strong>Quiz: </strong>You have written code to load some UIImages in the background (performSelectorOnBackgroundThread or NSInvocationOperation). The loading code calls the method below to resize the loaded image. This has &#8220;always working before&#8221;, but suddenly you keep getting a EXC_BAD_ACESS on the line marked. You have double checked all retains and even set NSZombieEnabled to YES because usually you get this message if you are using an overreleased object. But that did not fix the problem. What&#8217;s the reason for the crash and how would you fix it?</p>

<div class="wp_codebox"><table><tr id="p186612"><td class="code" id="p1866code12"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>image 
			  scaledToSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>newSize;
<span style="color: #002200;">&#123;</span>
	UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span> newSize <span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>image 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>,newSize.width,newSize.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;   <span style="color: #11740a; font-style: italic;">// &lt;- crash</span>
	UIImage<span style="color: #002200;">*</span> newImage <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>;
&nbsp;
	<span style="color: #a61390;">return</span> newImage;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p><strong>Answer:</strong> <span style="color: #fbfbfb;">UIKit is not thread-safe. Therefore all drawing via UIKit needs to happen on the main thread. The UIGraphicsBeginImageContext works to create the context, but the memory for it is owned to the main thread and trying to access it with drawInRect from another thread is causing this exception. To fix it you need to rewrite this method to create your own bitmap context on the non-main thread so that you then own the memory for it. After creating the bitmap context you draw the image to it with CGContextDrawImage and then create a new CGImage with CGBitmapContextCreateImage. From the CGImage you create a UIImage to return from the method.</span></p>
<hr />
<p><strong>Quiz: </strong>Off the top of your head: how many methods do you know of having a piece of code contained in one method being performed in the background? Which?</p>
<p><strong>Answer:</strong> <span style="color: #fbfbfb;">Correct Answers are: performSelectorOnBackgroundThread and creating an NSInvocationOperation from the selector as those work on an existing method. Creating an NSOperation sub-class or manual threading with NSThread are also valid answers, but those require that you move the code into a new class or need to be well versed with threading voodoo.<br />
</span></p>
<hr />
<p>So how many answers did you know? Be honest! If you also have a Cocoa riddle like these to contribute please mail them directly to me (oliver@drobnik.com) and I will publish them in this format.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2010/02/are-you-a-cocoa-crack-quiz-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>UIImageView + Touch Handling = UIButton</title>
		<link>http://www.drobnik.com/touch/2010/02/uiimageview-touch-handling-uibutton/</link>
		<comments>http://www.drobnik.com/touch/2010/02/uiimageview-touch-handling-uibutton/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 10:34:19 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=2054</guid>
		<description><![CDATA[gdscei asks:
"Hello, how can i make an image view work [...]]]></description>
			<content:encoded><![CDATA[<p>gdscei asks:</p>
<blockquote><p>&#8220;Hello, how can i make an image view work as a button? I want it to be assigned to the &#8216;reload&#8217; action of WebView.<br />
Can someone give me instructions how to do this?&#8221;</p></blockquote>
<p>Often people start out constructing their UI visually, starting with this line of reasoning &#8220;I want to show an image. Ah, UIImageView&#8221;. So they create the visual appearance of their UI either by clicking it together in Interface Builder or &#8211; if slightly more advanced in their coding skills &#8211; creating those image views in code.</p>
<p>So if we create a new view-based project in XCode, we could modify the viewDidLoad of the main view controller like this to show the image:</p>

<div class="wp_codebox"><table><tr id="p205413"><td class="code" id="p2054code13"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
	UIImageView <span style="color: #002200;">*</span>imageView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>
		CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100.0</span>, <span style="color: #2400d9;">100.0</span>, <span style="color: #2400d9;">57.0</span>, <span style="color: #2400d9;">57.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	imageView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Icon.png&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>imageView<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>imageView release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The next logical step in reasoning right after displaying the icon is now to get touch handling somehow. And that&#8217;s where you will get stuck because UIImageView is a dead end when it comes to reacting to the user&#8217;s finger.</p>
<p><span id="more-2054"></span><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>Sure, if you consider yourself extra smart and that you won&#8217;t be stumped by such an inconvenient &#8220;cul de sac&#8221; then you will proceed to subclass UIImageView and adding touch handling via the touchesBegan, touchesMoved, touchesCancelled and touchesEnded method. And don&#8217;t go telling me that you would never make such a mistake. I know, I did and I&#8217;ve seen smarter programmers than the both of us proceed to painstakingly code their own touch handling for UIImageViews.</p>
<p>This post is NOT about doing that. I cannot prevent you from scratching your left ear with your right index finger, but I CAN show a smarter way to get your own interactive custom buttons.</p>
<p>I already gave away half of the solution: CUSTOM BUTTONS. Apple made it confusing to beginners by having the default style for a button being a rounded rectangle with text inside. That&#8217;s why there is such a strong trend to using UIImageViews because beginners cannot imagine that their beautifully crafted button images would fit into this rounded container. But in fact the rounded rectangle style is only one of several possibilities.</p>
<p>UIButtonType can have these values:</p>
<ul>
<li>UIButtonTypeCustom,</li>
<li>UIButtonTypeRoundedRect</li>
<li>UIButtonTypeDetailDisclosure</li>
<li>UIButtonTypeInfoLight</li>
<li>UIButtonTypeInfoDark</li>
<li>UIButtonTypeContactAdd</li>
</ul>
<p>The very first on UIButtonTypeCustom will be our choice for making our own custom button. For all the other button types Apple provides the drawing, for the custom one you have to bring your own, preferably in the form of UIImages for the various states.</p>
<p>Let&#8217;s have a brief look at the inheritance of UIImageView versus UIButton. This kinship was not obvious to me, I only discovered it by accident when I also was in the situation that I wanted to touch-enable some subviews.</p>
<p>UIImageView inherits from UIView : UIResponder : NSObject<br />
UIButton inherits from UIControl : UIView : UIResponder : NSObject</p>
<p>So we can see that up to UIView both have the same set of methods and behaviors. An image view is just a UIView where an image from a property is being drawn in the view&#8217;s area, probably inside the drawRect. A button gains an additional level of inheritance: UIControl. If you read up on UIControl in the documentation it tells you that UIControl adds mechanisms to &#8220;convey user intent to the application&#8221;. It adds the target-action methodology to UIView where you can add targets and methods as actions that get executed when certain things happen. Like for example a touch is lifted inside a button or the value of a slider has changed.</p>
<p>But UIImageView and UIButton are views! Both have the genes of UIView inside of them and cannot deny their pedigree.</p>
<p>Knowing the above mentioned facts, why would you want to painstakingly code your own touch handling routines if you get them for free when choosing UIButton to display your icon image instead?</p>
<p>We can replace the above mentioned code with this:</p>

<div class="wp_codebox"><table><tr id="p205414"><td class="code" id="p2054code14"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    UIButton <span style="color: #002200;">*</span>imageButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeCustom<span style="color: #002200;">&#93;</span>;
    imageButton.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100.0</span>, <span style="color: #2400d9;">100.0</span>, <span style="color: #2400d9;">57.0</span>, <span style="color: #2400d9;">57.0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>imageButton setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Icon.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>imageButton<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Note that for buttons you generally don&#8217;t do alloc-initWithFrame, but use the factory method buttonWithType. Since this gives us an autoreleased button we don&#8217;t need to manually release it after addSubview.</p>
<p>If you do that you get exactly the same result, with a minor difference. If you tap the icon it darkens automatically to show the touch. That&#8217;s because UIButton expects to display something different when the button is touched. You can set a specific image for the highlighted state, if you don&#8217;t then UIButton takes the normal state image and darkens it automatically.</p>
<p>To disable this behavior change the property or set an image for the highlighted state:</p>

<div class="wp_codebox"><table><tr id="p205415"><td class="code" id="p2054code15"><pre class="objc" style="font-family:monospace;">imageButton.adjustsImageWhenHighlighted <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
<span style="color: #11740a; font-style: italic;">// or</span>
<span style="color: #002200;">&#91;</span>imageButton setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Icon_highlighted.png&quot;</span><span style="color: #002200;">&#93;</span> 
	forState<span style="color: #002200;">:</span>UIControlStateHighlighted<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Possible control states are:</p>
<pre>enum {
   UIControlStateNormal               = 0,
   UIControlStateHighlighted          = 1 << 0,
   UIControlStateDisabled             = 1 << 1,
   UIControlStateSelected             = 1 << 2,
   UIControlStateApplication          = 0x00FF0000,
   UIControlStateReserved             = 0xFF000000
};</pre>
<p>Therefore control state is more than a single value, it actually can also be a combination of several bits. The normal state is 0, the highlighted state is 1, disabled 2 and selected 4. You can specify any combination thereof by using the bitwise or operator |. For example to set an image for selected OR highlighted you use UIControlStateSelected|UIControlStateHighlighted. This possibility is not clear if you look at the inspector for a button in Interface Builder. That's why I prefer to do my buttons in code.</p>
<p>To clarify the states: A button at rest is "normal". As long as you put your finger down on it, it's "highlighted". If you lift again, it returns to "normal". If you set it's enabled property to NO, then it's "disabled". If you set it's selected property to YES, then it's "selected".</p>
<p>Cocoa Cracks NOTE that UIControlStateApplication actually shows us a range (16 bits) that we can use ourselves to define extra special states for our own controls. And UIControlStateReserved is a range (16 bits) that is reserved for Apple-internal use.</p>
<p>Ok, so much for states and looks. Adding targets and actions for the various possible interactions with a button is easy again:</p>

<div class="wp_codebox"><table><tr id="p205416"><td class="code" id="p2054code16"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>imageButton addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>buttonPushed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
	  forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>We also have to implement the "action" which is just a method. The target is the class instance where you implement the method, the action is a selector, i.e. the "fingerprint" of the method.</p>

<div class="wp_codebox"><table><tr id="p205417"><td class="code" id="p2054code17"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> buttonPushed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;It works!&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Note that a pointer to your button instance will be passed via the sender parameter. This can sometimes be useful, for example if you want to use a single method to respond to multiple buttons. In this case you would use the tag of the passed sender to discern which button the call was coming from.</p>
<p>This method of making images respond to touches works with very little extra tying. I'm using it in <a href="http://www.drobnik.com/touch/2010/02/dtcalendarviewcontroller-2-0/">DTCalendarView</a> for the individual days as well as for a full screen tap recognition in <a href="http://www.drobnik.com/touch/2010/02/dtsplashextender/">DTSplashExtender</a>. Best of all: it saves you lot of extra code or even having to subclass UIImageView.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2010/02/uiimageview-touch-handling-uibutton/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Double Tapping on Buttons</title>
		<link>http://www.drobnik.com/touch/2009/12/double-tapping-on-buttons/</link>
		<comments>http://www.drobnik.com/touch/2009/12/double-tapping-on-buttons/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 20:02:32 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=1777</guid>
		<description><![CDATA[Grinarn asks:
"I got several buttons set up on my view [...]]]></description>
			<content:encoded><![CDATA[<p>Grinarn asks:</p>
<blockquote><p>&#8220;I got several buttons set up on my view and when the button gets clicked, a detailed view of that item appears.<br />
What I need is another action method like double click or click and hold, to trigger another action.</p>
<p>How can I do this? I just found the events in the IB which seems only supports single touch events.&#8221;</p></blockquote>
<p>Any view in the SDK can receive and process touch events. This gives you the ability to implement any kind of tap or gesture that you might dream up. But for everyday purposes we will find the methods provided by UIControl sufficient. UIControl inherits from UIView which means that it can do everything that views can do, but it adds the Target-Action mechanism.</p>
<p>For this mechanism you can attach a multitude of various events to each control by simply specifying a target (= any object instance), an action (= any selector of the target) and a constant from the following list. &#8220;Selector&#8221; is only fancy name for method signature, which consists of the method name and the names of the parameters, all with a colon behind them.</p>
<p><strong>General Touch Actions</strong></p>
<ul>
<li>UIControlEventTouchDown</li>
<li>UIControlEventTouchDownRepeat</li>
<li>UIControlEventTouchDragInside</li>
<li>UIControlEventTouchDragOutside</li>
<li>UIControlEventTouchDragEnter</li>
<li>UIControlEventTouchDragExit</li>
<li>UIControlEventTouchUpInside</li>
<li>UIControlEventTouchUpOutside</li>
<li>UIControlEventTouchCancel</li>
</ul>
<p><strong>Specific to Editing Controls</strong></p>
<ul>
<li>UIControlEventValueChanged</li>
<li>UIControlEventEditingDidBegin</li>
<li>UIControlEventEditingChanged</li>
<li>UIControlEventEditingDidEnd</li>
<li>UIControlEventEditingDidEndOnExit</li>
</ul>
<p><strong>Generic Constants matching several Actions</strong></p>
<ul>
<li>UIControlEventAllTouchEvents</li>
<li>UIControlEventAllEditingEvents</li>
<li>UIControlEventApplicationReserved</li>
<li>UIControlEventSystemReserved</li>
<li>UIControlEventAllEvents</li>
</ul>
<p>Now generally if you make a button then you would use the UIControlEventTouchUpInside event even though at first you might instinctively go for UIControlEventTouchDown. TouchUpInside is the standard as it allows the user to reconsider and move outside of the button before lifting his finger thus cancelling his action. Otherwise the button would be like a landmine where there is no way back after touching it.</p>
<p>Now there might be cases where you exactly WANT the action to be fired right when you touch the control. Then TouchDown is the right action. You also see a TouchDownRepeat action available, but this always comes in succession after a TouchDown. Therefore some additional trickery is necessary to be able to distinguish between single and double tapping a button.</p>
<p><span id="more-1777"></span></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>The coolest way to do that that I have seen so far is by means of firing a delayed performSelector and canceling this if there is a TouchDownRepeat before it fires. For this example we set up a label and a button in a viewControllers viewDidLoad:</p>

<div class="wp_codebox"><table><tr id="p177718"><td class="code" id="p1777code18"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad 
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// label is defined in header so that we can access it later</span>
	label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">80.0</span>, <span style="color: #2400d9;">70.0</span>, <span style="color: #2400d9;">120.0</span>, <span style="color: #2400d9;">20.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>label<span style="color: #002200;">&#93;</span>;
&nbsp;
	UIButton <span style="color: #002200;">*</span>button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeRoundedRect<span style="color: #002200;">&#93;</span>;
	button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">100.0</span>, <span style="color: #2400d9;">100.0</span>, <span style="color: #2400d9;">80.0</span>, <span style="color: #2400d9;">50.0</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>button setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tap Me!&quot;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// register target-actions for single and repeated touch</span>
	<span style="color: #002200;">&#91;</span>button addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>touchDown<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchDown<span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#91;</span>button addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>touchDownRepeat<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchDownRepeat<span style="color: #002200;">&#93;</span>;	
&nbsp;
	<span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>button<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</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>dealloc 
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>label release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>And somewhere at the end of the view controller we put this code:</p>

<div class="wp_codebox"><table><tr id="p177719"><td class="code" id="p1777code19"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark Actions</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> singleTapOnButton<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	label.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Single Tap&quot;</span>;
<span style="color: #002200;">&#125;</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> doubleTapOnButton<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	label.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Double Tap&quot;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark Button UIControl Actions</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> touchDown<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Touch Down&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// give it 0.2 sec for second touch</span>
	<span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>singleTapOnButton<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>sender afterDelay<span style="color: #002200;">:</span><span style="color: #2400d9;">0.2</span><span style="color: #002200;">&#93;</span>;  
<span style="color: #002200;">&#125;</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> touchDownRepeat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span style="color: #400080;">NSObject</span></a> cancelPreviousPerformRequestsWithTarget<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>singleTapOnButton<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> object<span style="color: #002200;">:</span>sender<span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Touch Down Repeat&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>self doubleTapOnButton<span style="color: #002200;">:</span>sender<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Note the (id)sender parameter for actions which are used with this mechanism. Due the object-oriented nature of this all it&#8217;s actually the button or control itself sending the event. Or more precisely it calls these methods on the main thread and passes its self-reference as the first parameter. So you can always type cast the sender to the kind of class you expect to cause such an event and then access properties. One trick that I like to often to is to use the tag property of buttons so that I can have a single action for multiple buttons and decide which button it was for by looking at the tag.</p>
<p>The magic happens in the touchDown method. There you see that the method singleTapOnButton is put on delayed execution. Code execution does not pause here and the UI continues to be responsive until the time interval expires and the selector is called on the main thread. I found 0.2 seconds to be sufficient, but it can be any length you like. If there is only a single touch and not a repeat touch then that&#8217;s it, the selector is called and the label is set to show that we detected a single tap. If the user taps a second time within these 0.2 seconds then the previous delayed performSelectors are all cancelled and instead the doubleTapOnButton method is being called. </p>
<p>Now that was too easy, permit me to get your head spinning with the real Christmas Surprise I have in stock for you, freshly baked out of Dr. Touch&#8217;s Cocoa Kitchen. If you are using double taps all the time then you can also modify the sending behavior of UIButtons to always delay the calling of the method for TouchDown. Simple add the following header and implementation to your project.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p><strong>UIButton+DoubleTapping.h</strong></p>

<div class="wp_codebox"><table><tr id="p177720"><td class="code" id="p1777code20"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> UIButton <span style="color: #002200;">&#40;</span>DoubleTapping<span style="color: #002200;">&#41;</span> 
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>UIButton+DoubleTapping.m</strong></p>

<div class="wp_codebox"><table><tr id="p177721"><td class="code" id="p1777code21"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;UIButton+DoubleTapping.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UIButton <span style="color: #002200;">&#40;</span>DoubleTapping<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> delayedSendAction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parameters
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// unpack parameters</span>
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>actionString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>parameters objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;action&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">SEL</span> action <span style="color: #002200;">=</span> NSSelectorFromString<span style="color: #002200;">&#40;</span>actionString<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">id</span> target <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>parameters objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;target&quot;</span><span style="color: #002200;">&#93;</span>;
	UIEvent <span style="color: #002200;">*</span>event <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>parameters objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;event&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// now actually send the action</span>
	<span style="color: #002200;">&#91;</span>super sendAction<span style="color: #002200;">:</span>action to<span style="color: #002200;">:</span>target forEvent<span style="color: #002200;">:</span>event<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</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>sendAction<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">SEL</span><span style="color: #002200;">&#41;</span>action to<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>target forEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// find out if this is a first tap</span>
&nbsp;
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>actionString <span style="color: #002200;">=</span> NSStringFromSelector<span style="color: #002200;">&#40;</span>action<span style="color: #002200;">&#41;</span>;
&nbsp;
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>touchDownMethodName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self actionsForTarget<span style="color: #002200;">:</span>target forControlEvent<span style="color: #002200;">:</span>UIControlEventTouchDown<span style="color: #002200;">&#93;</span> lastObject<span style="color: #002200;">&#93;</span>;
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>touchDownRepeatMethodName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self actionsForTarget<span style="color: #002200;">:</span>target forControlEvent<span style="color: #002200;">:</span>UIControlEventTouchDownRepeat<span style="color: #002200;">&#93;</span> lastObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// we assume that there is only one action registered for this control event type</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>touchDownMethodName isEqualToString<span style="color: #002200;">:</span>actionString<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;">// we delay first touches</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// package everything in dictionary so that we can pass it as single parameter</span>
		<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> <span style="color: #002200;">*</span>tmpDict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/"><span style="color: #400080;">NSDictionary</span></a> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>actionString, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;action&quot;</span>,
								 target, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;target&quot;</span>, event, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;event&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>self performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>delayedSendAction<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>tmpDict afterDelay<span style="color: #002200;">:</span><span style="color: #2400d9;">0.2</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">return</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>touchDownRepeatMethodName isEqualToString<span style="color: #002200;">:</span>actionString<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;">// Double Touch, we cancel the delayed request</span>
		<span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span style="color: #400080;">NSObject</span></a> cancelPreviousPerformRequestsWithTarget<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// all other events we simple pass on</span>
	<span style="color: #002200;">&#91;</span>super sendAction<span style="color: #002200;">:</span>action to<span style="color: #002200;">:</span>target forEvent<span style="color: #002200;">:</span>event<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Now let this melt in your mouth for the full flavor. This makes use of the following special ingredients:</p>
<p>You can override SDK class methods by simple creating a category and then just putting in the methods you want to override. These changes are global, so you don&#8217;t even need to add this header to your code. It simply being there modifies the behavior of all UIButtons.</p>
<p>The sendAction method does not tell you the UIControlEvent constant it is being called for, but instead a UIEvent with lots of useless information. But we don&#8217;t mind, because we can get the name of the method that is specified as action by calling actionsForTarget:forControlEvent. Because we usually only have one action for a certain type it is sufficient to use lastObject for the array this returns.</p>
<p>For the sendAction we need 3 parameters, performSelector:withObject:afterDelay: only takes one. So we have to package it all in a dictionary. A selector parameter of type SEL cannot itself be put into this dictionary, but we can use NSStringFromSelector() to convert it into an NSString which will do just fine in there. To reverse this we use NSSelectorFromString(). </p>
<p>Finally we only want to delayed calling of the TouchDown method be cancelled if we encounter a TouchDownRepeat. Otherwise the TouchUpInside would also cancel the TouchDown. That&#8217;s why we need to know the method name of the touch down method as well.</p>
<p>All this simplifies your code in the view controller.</p>

<div class="wp_codebox"><table><tr id="p177722"><td class="code" id="p1777code22"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark Button UIControl Actions</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> touchDown<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	label.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Single Tap&quot;</span>;
<span style="color: #002200;">&#125;</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> touchDownRepeat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	label.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Double Tap&quot;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Bear in mind that now you have to use TouchDown for single taps and TouchDownRepeat for double taps. Were you to link the single tap action to the TouchUpInside (as recommended) then this does not work, because you would get the TouchDownRepeat message immediately followed by a TouchUpInside. So you would have to maybe have a BOOL flag track if you have already reacted to the double touch and if YES then ignore the TouchUpInside.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2009/12/double-tapping-on-buttons/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iTunes Release Dates</title>
		<link>http://www.drobnik.com/touch/2009/12/itunes-release-dates/</link>
		<comments>http://www.drobnik.com/touch/2009/12/itunes-release-dates/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 18:47:47 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=1691</guid>
		<description><![CDATA[Mingleboy asked Apple via E-Mail:
"Why does my updated [...]]]></description>
			<content:encoded><![CDATA[<p>Mingleboy asked Apple via E-Mail:</p>
<blockquote><p>&#8220;Why does my updated app not appear amongst the new apps even though I changed the release date on iTunes Connect?&#8221;</p></blockquote>
<p>We all remember that previously it was possible to hop to the first pages of iTunes by changing this date. And of course this &#8220;feature&#8221; was exploited quite a bit by developers hoping to achieve additional attention for their apps and thus additional sales.</p>
<p>Apple recently fixed this to match what they originally intended, it appears now that it was a bug in the system anyway that Apple willingly ignored for some time until the gaming of release dates overboarded. I reported about this change in <a href="http://www.drobnik.com/touch/2009/11/dr-touch-001/">Episode 1 of the Dr. Touch Podcast</a>.</p>
<p><span id="more-1691"></span></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>What&#8217;s interesting in this special case is that Apple Developer Relations actually responded with the official explanation which Mingleboy <a href="http://www.iphonedevsdk.com/forum/iphone-sdk-development/35037-application-update-strange-mail-apple.html">posted on the iphonedevsdk forum</a>.</p>
<blockquote><p>&#8220;The released date displayed on an application page is the date the latest version of that application went live on the App Store. This date will allow customers to be aware of the date this version was released.</p>
<p>On category pages on the App Store, when you choose to sort by Release Date, applications are sorted according to the date the first version of the app went live on the App Store. Sorting by the original app released date creates a list where customers can easily find the most recent additions to the App Store.</p>
<p>Both of these dates are automatically set in the iTunes system and cannot be edited in iTunes Connect.</p></blockquote>
<p>Clear as mud. Works as designed &#8230; originally &#8230; a year ago.</p>
<p>The date you can set on iTunes Connect was only ever meant to prevent availability of your app TOO EARLY, for example if you want the release to coincide with a marketing campaign or other date. Though nobody I know of ever actually used this date because of the long time (2 weeks average) it takes to get approved. If I have to wait for 2 weeks until I can sell something, why would I want to wait artificially longer?</p>
<p>This date still does that, delay the release even though it has already been approved. But now it does not modify the release date on the store any more.</p>
<p>As of now you no longer have any means by which you can boost your app&#8217;s visibility on the app store. The only stroke of luck would be to get featured by Apple but the chances of that are relatively slim unless your app does something really great or comes from a big development house.</p>
<p>So the date order on app store categories really DOES show you apps that where approved just now. Really fresh apps, not old stinking fish that where taken from the back and relabeled as &#8220;brand spanking new&#8221;.</p>
<p>That leaves the question how to grab more attention and thus more sales? Here are some ideas:</p>
<ul>
<li>Have the best possible key words. Don&#8217;t use other apps names though, because this will get your app rejected. Use terms that are highly relevant to your app.</li>
<li>Post a video of your app in action on YouTube where you show off features that you are especially proud of.</li>
<li>Blog and tweet about updates and if you drop the price for a limited period of time. Take part in activities like one of those advent calendars I keep reporting about on the Dr. Touch Podcast.</li>
<li>Sponsor an ad on the Dr. Touch Podcast. <img src='http://www.drobnik.com/touch/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </li>
<li>Continue to do updates to at least show people who arrive at the app page that the app is constantly improving, meaning more and more value for their money.</li>
<li>Take what you learned and make a NEW APP. Once your app sales are on the long tail you can forget about affecting this course in any serious way. During the day that your new app is legitimately in front, use all means at your disposal to get people to notice it. Get bloggers to review, twitter about it, do all sorts of attention grabbing stunts on the regular Internet.</li>
<li>Think of something new. You where creative enough to publish an app. Why not be really creative in how to get people to purchase it?</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2009/12/itunes-release-dates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The World on an NSString</title>
		<link>http://www.drobnik.com/touch/2009/11/the-world-on-an-nsstring/</link>
		<comments>http://www.drobnik.com/touch/2009/11/the-world-on-an-nsstring/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 06:58:58 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=1626</guid>
		<description><![CDATA[When you are are newbie in programming objective-C then [...]]]></description>
			<content:encoded><![CDATA[<p>When you are are newbie in programming objective-C then you might find somethings confusing when you start using strings. Coming from C you where used to using zero terminated C-Strings. Coming from other languages you might be challenged by the fact that there is no implicit type conversion like, for example, in BASIC.</p>
<p>In regular C strings are pointers of type &#8220;char *&#8221;, meaning that it&#8217;s the memory address of a one byte character. The length of a C-String is determined by a binary zero &#8216;\0&#8242; at the end of it. Objective-C rarely uses those, instead NSString means the world to us.</p>
<p>The core fundamental to realize first is that you are always dealing with pointers &#8211; that is addresses in memory &#8211; when using objects (instances of classes). So it simply does not make sense to compare strings with the == operator. Two variables pointing to NSString might or might not actually point to the same instance. (Actually the same was true for C-Strings, because the same text might or might not be contained in different memory regions referenced by char * pointers)</p>
<p><span id="more-1626"></span></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>

<div class="wp_codebox"><table><tr id="p162623"><td class="code" id="p1626code23"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>oneString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;one&quot;</span>;
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>twoString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, oneString<span style="color: #002200;">&#93;</span>;
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>threeString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithCString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;one&quot;</span> encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>fourString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;one&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// fourString is optimized to be the same instance as oneString</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;strings 1: %@, 2: %@, 3: %@, 4: %@&quot;</span>, oneString, twoString, threeString, fourString<span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;address 1: 0x%x, 2: 0x%x, 3: 0x%x, 4: 0x%x&quot;</span>, oneString, twoString, threeString, fourString<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>From this example you see some different methods of initializing a string. Actually all of these are constants which get autoreleased when you leave their scope. The first uses the shorthand @, the second the NSString class method stringWithFormat and the third another class method to convert a C-String into an NSString. </p>
<p>The fourth one is of special interest to us. It turns out the compiler optimizes oneString and fourString to point to the very same memory as proven by the address. This is done for performance reasons but in no way are you allowed to rely on this happening. So this is the one example where == would actually work for comparing strings, if you are only comparing constants. But it&#8217;s smarter to always deal with strings in a manner that works for all of them.</p>
<p>This brings us to two methods of comparison that I have seen so far. And gotten confused by:</p>

<div class="wp_codebox"><table><tr id="p162624"><td class="code" id="p1626code24"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// isEqual expects any object, true if you pass a string that also compares true</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>oneString isEqual<span style="color: #002200;">:</span>twoString<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;isEqual really compares hashes %d &lt;-&gt; %d&quot;</span>, <span style="color: #002200;">&#91;</span>oneString hash<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>twoString hash<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>oneString isEqualToString<span style="color: #002200;">:</span>threeString<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;the normal method of comparing is isEqualToString&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Using the instance method isEqualToString: is the obvious choice you know from RTFM. But sometimes we also see isEqual:, is this legal? Until I knew better I would go and replace all isEqual: with isEqualToString: to be safe. Don&#8217;t really know what the shorter one does and also why not use the intended method.</p>
<p>If you dig in the documentation you find that isEqual is part of the NSObject protocol. The default implementation compares the integer value returned from [myObject hash], which every object has. This is ordinarily used when adding objects to container classes but isEqual also determines that two objects are equal if their hashes are equal. </p>
<p>NSString overrides hash such that the same text will have the same hash. Therefore isEqual will work in the majority of cases when comparing NSString. isEqual does not care which class you pass as parameter, the parameter is of type &#8220;id&#8221;. It can ignore the class because there is no comparison taking place except of hash values. So one might argue that isEqual is potentially faster than having to compare strings character by character every time. Though I have yet to see a case where one would prefer such trickery that might or might not give you a performance benefit over safety and readability of your code.</p>
<p>The same is also true for the mutable version of NSString: NSMutableString. If you modified any string, the hash changes and therefore isEqual continues to work. See the example below to see it working. So we can infer that hash is either modified every time a string is changed or at least once you query it.</p>

<div class="wp_codebox"><table><tr id="p162625"><td class="code" id="p1626code25"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/"><span style="color: #400080;">NSMutableString</span></a> <span style="color: #002200;">*</span>oneString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/"><span style="color: #400080;">NSMutableString</span></a> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;one&quot;</span><span style="color: #002200;">&#93;</span>;
<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>twoString <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;one&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// hashes are same</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hashes: 1:%d 2:%d&quot;</span>, <span style="color: #002200;">&#91;</span>oneString hash<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>twoString hash<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>oneString appendString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;more&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// modifying mutable string also modifies hash</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hashes: 1:%d 2:%d&quot;</span>, <span style="color: #002200;">&#91;</span>oneString hash<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>twoString hash<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// remove appended string again</span>
<span style="color: #002200;">&#91;</span>oneString deleteCharactersInRange<span style="color: #002200;">:</span>NSMakeRange<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">3</span>, <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// hashes are same again</span>
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hashes: 1:%d 2:%d&quot;</span>, <span style="color: #002200;">&#91;</span>oneString hash<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>twoString hash<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>But even knowing this stuff does not change the fact that using isEqualToString is bad. Actually I continue to use it as the sole trusted comparator for all my string needs. I don&#8217;t trust this hash voodoo.</p>
<p>Finally it weren&#8217;t a post by me if I didn&#8217;t think of a way to use a class category as well. How about comparing a string with an integer? There are two ways to go about this: convert the string to integer and compare it, or to have a class category for it.</p>
<p>For the first &#8220;usual&#8221; method we use the NSString intValue instance method.</p>

<div class="wp_codebox"><table><tr id="p162626"><td class="code" id="p1626code26"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>one <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;123&quot;</span>;
NSInteger i <span style="color: #002200;">=</span> <span style="color: #2400d9;">123</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>one intValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">==</span>i<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Same&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>For the second &#8220;sophisticated&#8221; method we extend NSString to also know how to do comparisons with integers.</p>
<p><strong>NSString+integer.h</strong></p>

<div class="wp_codebox"><table><tr id="p162627"><td class="code" id="p1626code27"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">&#40;</span>integer<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isEqualToInteger<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>i;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p><strong>NSString+integer.h</strong></p>

<div class="wp_codebox"><table><tr id="p162628"><td class="code" id="p1626code28"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;NSString+integer.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">&#40;</span>integer<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isEqualToInteger<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>i
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self intValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">==</span>i;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>This packages the ugliness away into a separate file and henceforth we can use our pretty comparator like this:</p>

<div class="wp_codebox"><table><tr id="p162629"><td class="code" id="p1626code29"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>one <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;123&quot;</span>;
NSInteger i <span style="color: #002200;">=</span> <span style="color: #2400d9;">123</span>;
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>one isEqualToInteger<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Same&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In the same way to can construct comparators for any kind of parameter be it scalar or object pointers. Of course the above mentioned category could also instead take an NSNumber. You would check that it&#8217;s really an NSNumber being passed by checking the class at the top of the method and return NO if it&#8217;s not a descendant of NSNumber.</p>
<p>But this is left as an exercise to you. Do you know of any other traps related to NSString that I have yet to discover? Let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2009/11/the-world-on-an-nsstring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Asking Users for a Review</title>
		<link>http://www.drobnik.com/touch/2009/11/asking-user-for-review/</link>
		<comments>http://www.drobnik.com/touch/2009/11/asking-user-for-review/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 17:00:53 +0000</pubDate>
		<dc:creator>drops</dc:creator>
				<category><![CDATA[Q&A]]></category>

		<guid isPermaLink="false">http://www.drobnik.com/touch/?p=1597</guid>
		<description><![CDATA[

Yves Gonzales asked:
"Would you know what the URL  [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-1599" style="margin-left: 10px; margin-bottom: 10px;" title="Reviews" src="http://www.drobnik.com/touch/wp-content/uploads/2009/11/reviews.jpg" alt="Reviews" width="192" height="288" /></p>
<p>Yves Gonzales asked:</p>
<blockquote><p>&#8220;Would you know what the URL scheme is for writing a review in the AppStore, launched from within an app in iPhone, which opens AppStore? (I want to ask users to leave a review.)&#8221;</p></blockquote>
<p>At first I answered that I did not think this was possible. But Yves, with the help of trusty Mr. Google discovered a better answer than mine. There is in fact a possibility to get the mobile App Store app to open on the review page for a specific app.</p>
<p><span id="more-1597"></span></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-1142183725909145";
google_ad_slot = "4110438702";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>The solution was documented by <a href="http://www.memention.com/blog/2009/09/03/Open-Reviews.html">Edward Patel</a> on his blog. I replaced the app id with mine and the result is that the app closes and the mobile App Store opens showing the corresponding review page.</p>

<div class="wp_codebox"><table><tr id="p159730"><td class="code" id="p1597code30"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa&quot;</span>;
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@/wa/viewContentsUserReviews?&quot;</span>, str<span style="color: #002200;">&#93;</span>;
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@type=Purple+Software&amp;id=&quot;</span>, str<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Here is the app id from itunesconnect</span>
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@308590265&quot;</span>, str<span style="color: #002200;">&#93;</span>; 
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> openURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> URLWithString<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>At the top of the review page there is button that allows the customer to leave the coveted review.</p>
<p>Yves put this code into his own to automatically present an alert view to the user after several starts of the app to <span style="text-decoration: line-through;">nag</span> kindly ask for a review. Upon launching of the app you would call the reviewCounter method. This gets the number of launches so far and on the third launch it presents such an alert view.</p>
<p><a href="http://www.drobnik.com/touch/wp-content/uploads/2009/11/review_dialog.png"><img class="size-full wp-image-1601 alignnone" title="Review Dialog" src="http://www.drobnik.com/touch/wp-content/uploads/2009/11/review_dialog.png" alt="Review Dialog" width="171" height="177" /></a></p>
<p>Here&#8217;s the code for that, courtesy of Yves. I cleaned it up a bit and removed NSLogs.</p>

<div class="wp_codebox"><table><tr id="p159731"><td class="code" id="p1597code31"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> reviewCounter
<span style="color: #002200;">&#123;</span>
	CGFloat reviewInt <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/"><span style="color: #400080;">NSUserDefaults</span></a> standardUserDefaults<span style="color: #002200;">&#93;</span> integerForKey<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;intValueKey&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>reviewInt<span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		reviewInt<span style="color: #002200;">++</span>;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/"><span style="color: #400080;">NSUserDefaults</span></a> standardUserDefaults<span style="color: #002200;">&#93;</span> setInteger<span style="color: #002200;">:</span>reviewInt forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;intValueKey&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span> 
	<span style="color: #a61390;">else</span> 
	<span style="color: #002200;">&#123;</span>
		CGFloat start <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
		<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/"><span style="color: #400080;">NSUserDefaults</span></a> <span style="color: #002200;">*</span>reviewPrefs <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/"><span style="color: #400080;">NSUserDefaults</span></a> standardUserDefaults<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>reviewPrefs setInteger<span style="color: #002200;">:</span>start forKey<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;intValueKey&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>reviewPrefs synchronize<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// writes modifications to disk</span>
	<span style="color: #002200;">&#125;</span> 
&nbsp;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>reviewInt <span style="color: #002200;">==</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Mabuhay!&quot;</span> message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Looks like you Enjoy using this app. Could you spare a moment of your time to review it in the AppStore?&quot;</span>
													   delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> otherButtonTitles<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OK, I'll Review It Now&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Remind Me Later&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Don't Remind Me&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>; 
		<span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
&nbsp;
		reviewInt<span style="color: #002200;">++</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/"><span style="color: #400080;">NSUserDefaults</span></a> standardUserDefaults<span style="color: #002200;">&#93;</span> setInteger<span style="color: #002200;">:</span>reviewInt forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;intValueKey&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #6e371a;">#pragma mark alert view delegate</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>alertView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAlertView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>actionSheet clickedButtonAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>buttonIndex
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// the user clicked one of the OK/Cancel buttons</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>buttonIndex <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
&nbsp;
		<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa&quot;</span>;
		str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@/wa/viewContentsUserReviews?&quot;</span>, str<span style="color: #002200;">&#93;</span>; 
		str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@type=Purple+Software&amp;id=&quot;</span>, str<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Here is the app id from itunesconnect</span>
		str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@308590265&quot;</span>, str<span style="color: #002200;">&#93;</span>; 
&nbsp;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> openURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/"><span style="color: #400080;">NSURL</span></a> URLWithString<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>buttonIndex <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: #a61390;">int</span> startAgain <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/"><span style="color: #400080;">NSUserDefaults</span></a> standardUserDefaults<span style="color: #002200;">&#93;</span> setInteger<span style="color: #002200;">:</span>startAgain forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;intValueKey&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>buttonIndex <span style="color: #002200;">==</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span> 
		<span style="color: #a61390;">int</span> neverRemind <span style="color: #002200;">=</span> <span style="color: #2400d9;">4</span>;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/"><span style="color: #400080;">NSUserDefaults</span></a> standardUserDefaults<span style="color: #002200;">&#93;</span> setInteger<span style="color: #002200;">:</span>neverRemind forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;intValueKey&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The theory behind this approach is that users would generally forget to leave a review because it is not part of the regular flow of things from finding the app, purchasing and trying it out. </p>
<p>Do you think this will get more people to comment favorably on your app if you ask them for it? Will more reviews mean more sales? Comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.drobnik.com/touch/2009/11/asking-user-for-review/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
