Our DNA is written in Objective-C

Defaults for the Defaults

I previously explained how to use NSUserDefaults to your advantage, basically being the OSX equivalent to the Windows registry. User Defaults are the way how you can persist small amounts of data in a convenient way. They are basically dictionaries that the system takes care of for you with the added benefit of being cached in memory.

Today we’ll up it one notch. Session 124 of the WWDC 2010 videos brought to my attention the fact that there’s also a mechanism to provide default values for the defaults. Up until now I would have retrieved an object from the defaults, checked if it’s nil and then set the value. Consider the following code snipped from SpeakerClock:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *countdownNum = [defaults objectForKey:@"Countdown"];
if (countdownNum)
	countdown = [countdownNum doubleValue];
	countdown = self.currentPreset.startDuration;

This works, but it’s not elegant. Wouldn’t it be great if we could pre-register those values so that a simple retrieval always has a result?

Read more

Dr. Touch #18 – “Bar None”

The iPhone 4 rulez, bar none.


Show notes, aka “my script” after the break.

Read more

Microsoft Wooing iOS Developers

Yesterday I found an e-mail in my inbox that gave me a bit of a spooky feeling in my stomach. Ordinarily such a mail would have quickly ended up in my trash if it weren’t for the details they put in there.

Why would Microsoft be contacting me? They obviously did a bit of digging or reading to know that I created the apps for CCS Publishing. They didn’t bother to check on my references page though. The ACLS app was done by somebody else. But still it shows a fair amount of data mining as they appear to have checked those apps’ rankings and then cross referenced them with who was the real author.

Read more

Understanding iOS 4 Backgrounding and Delegate Messaging

UPDATE: Added handleOpenURL to the flow charts. Added UIApplicationExitsOnSuspend. Untangled some lines.

UPDATE: renamed deprecated handleOpenURL to newer name.

Now that we all are moving our source code gradually to iOS 4 I had to pause and think a bit about where to move which code. A problem that I’m facing frequently when updating a project is that the didFinishLaunching is only called if the app really launches.

That poses a bit of a challenge if you are used to doing things like refreshing images or other files off the internet. An app that is resumed from standby does no longer go through this delegate method. So an app that would always show fresh content upon launch before 4.0 multitasking would no longer load any new content as soon as you build it for 4.0. That’s actually one of the main reasons why I have not yet had time to update MyAppSales to 4.0.

To gain the possibility for “fast app switching” you actually don’t need to do anything. All apps automatically support it because they no longer get terminated if the user pushes the Home button. They get put into a sleep mode while the iPhone still has enough memory for everything else. It’s only if RAM runs out that the OS starts killing apps. There is begins with the ones that have the most memory reserved.

I grabbed the free trial of Omnigraffle and the Non-techie Process Flowchart Stencils by gfraser. Then I researched when all these various delegate methods of UIApplication are being called and drew charts to illustrate the flow.

By inhaling first how it was before multitasking and then upgrading your mental process to backgrounding we can begin to fully appreciate how it all fits together.

Read more

The 3.2 Hurdle of MPMoviePlayerController

Prior to iOS 3.2 your only option for playing videos on iPhone was to use MPMoviePlayerController and only in full screen mode. It made sense because who really wants to look at stamp sized videos on his mobile phone.

The iPad changed all that and brought along several changes in how you can place videos. All those changes also made it into the iPhone SDKs. Here’s a pitfall that you might not have seen if you didn’t upgrade the SDK on one of your iPhone projects.
Read more

Drawing on UIImages

Jayesh asks:

Thanks for your article on UIImage from UIView.

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.

Basically I will show image and put circle / rectangle on image showing area in blue print and save it.

How should I do that? Can you please suggest approach, sample codes etc?

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.

Read more

iPhone 4 Landing in Austria

It’s one of these days there the arrival of a new and shiny Apple toy is diminishing productivity to zero. My iPhone 4 arrived at lunchtime and so I spent my afternoon unboxing and setting up my new iDevice.

I had purchased a black 32 GB iPhone 4 on Apple’s UK store. The kingdom is a tier 1 country which got it at the same time as the US. What’s also great is that the empire’s Apple online store sells the variant without SIM-lock. A necessity if you want to be able to use the iPhone 4 as a phone in your home cellular network. Since I ordered Apple added this paragraph to clarify this:

When you purchase your iPhone from the Apple Online Store, you’ll get it SIM-free. So you can sign up for service with the carrier of your choice and change your carrier at any time

My friend Michael Kaye in London purchased the phone for me (32 GB for 599 GBP) and shipped it via parcel service to Austria. This cost me another 60 pounds, but I had insurance and tracking. The latter was kind of weird, because the original carrier Parcelforce handed off the package to FedEx and so I got incomplete and somewhat strange tracking info. But fortunately it turned out fine.

I’m unboxing the iPhone after the back, check out my YouTube video!

Read more

Hacking UIScrollView Gesture Recognizers

As of SDK 3.2 most of the touch handling code in stock controls has been taken out and replaced with this amazing new technology called Gesture Recognizers. This means besides of using them yourself and creating your own you can also fiddle with behaviors of standard controls if they interfere with your own gestures.

I’m currently working quite a bit on something based on UIScrollView and there I found several things that I needed to tweak.

Disabling Pinch

The first modification I did was to get around a bug in UIScrollView.  I did not actually want user zooming in my scroll view, but just use the setting of the zoomLevel to scale images without having to redraw them. And you can only set the zoomLevel to values between min and max.

So, during autorotation, I reduced the min zoom scale, set the new zoom level and then set min and max both to the new zoom. But the problem with this, as of SDK 3.2., is that if you change the min or max zoomLevel property the scroll silently adds a UIPinchGestureRecognizer to itself via the addGestureRecognizer method that now all views have.
Read more

Service Announcement: Upgrading iOS 4 GM

A not-to-be-named friend asked me:

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’t have the game center, but I don’t use that anyway. I am afraid if I don’t remove, the software will expire (happened on beta 4 and then I couldn’t use the phone (or back up first) until I could load the latest.

If you’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.

Wait! That’s a trick question…!

Read more

BOOK: The Business of iPhone App Development

A long time ago, in a galaxy far far away … I think around the beginning of the year actually, Michael Schneider asked me to read this book that he collaborated on with Dave Wooldrige.

At this time and until this date this book remains the biggest and most comprehensive guide on what traps there are in the iPhone app business. It took me some time to read the book from cover to cover and then I also was simply to busy and lazy to do this review. And actually there was another reason, that I din’t want to admit to.

I’m by nature an iPhone app creator, not a business person. I had my first app in the store about 2 months after downloading the SDK back in 2008. Making apps is what I love, but marketing them… yuck! Now if this book really was as good as the table of contents looks then this would mean that I had found – rather by accident on twitter – the ultimate guide to everything that I needed to know about transforming my app workshop into a viable business.

Read more