If you like my tutorials, you will love my book . It is chockful of advanced programming techniques and the only comprehensive barcode reference for serious iOS developers.
Our DNA is written in Objective-C

Category Archive for ‘Recipes’ rss

Deep-Copying Dictionaries

Cocoa Touch has this concept of basic functionality being immutable and then there is a bigger brother of most classes that adds mutability. One effect of this pradadigm is that often you reuse a single NSString throughout your code because you pass around a pointer to it, retaining it wherever you are interested in it’s continued existence in memory.

Only very rarely you would see the use of the [receiver copy] approach which is like an init but creates a copy of the object. As I said this rarely makes sense because if the original is non-mutable in the first place why do I need to clone it? If it is used in another place and then there it is released and some other static value retained it does not have any adverse effect on other places where it is used, provided that proper retaining occurs.

But this feature is there because there might really BE some esotheric cases. There is even the “copy” attribute for properties which sets up your setters to copy the incoming objects, as opposed to the “retain” attribute which simply retains it.

Read more

Developing iPhone Apps on Snow Leopard

I was quite looking forward to getting a chance to improve my already quite satisfying Mac experience to the latest iteration of the operating system. Having bought my new MacBook Pro after Juni, I was elegible for an extra cheap upgrade. So I filled in my serial number and got promised a delivery about a week afterwards. But not being the patient type I figured it would not be a sin to borrow a friend’s DVD and install the new cat. Hey, I own a license, who cares which media I’m using?

Installation was as painless as can be. You pop in the DVD while your Mac is running, click a couple of times and then you wait. After 20% there is a first reboot, then you wait, all together roughly 40 minutes. At first you don’t see any new stuff, except the same welcome Animation playing in a Window instead of full screen. But then you start to see little things. Many little things, in fact enough to warrant a 23-page in- depth technical review on Ars Technica which is great bedside reading making you dream nicely about your invigorated Mac.

I was excited to install the new Xcode 3.2 from the “Optional Installs” folder on the DVD, then I downloaded and installed the latest iPhone 3.1 SDK for Snow Leopard which in contrast to Leopard is just the SDKs and now Xcode. The Leopard version contains Xcode 3.1.4, which for strange reasons does kind of run, but is “not officially supported by Apple to run on Snow Leopard”. Still, I will show you how you CAN compile against a 2.x SDK. After that I will show you Apple really wants you to do, but leaves it up to SDK diggers to tell the public.

Read more

Love to be Notified!

One of the techniques which I have found to be extremely useful are notifications in Objective-C. Using NSNotification to send broadcasts to all objects in your app is something that I did not see in any other language or framework I visited before. Here is a quick recipe to show how to use them.

Think of notifications as a method of informing all your class instances within your app at the same time. Sometimes there is a global change that does not have a single target like you would for a delegate. Without notifications you would have to keep track of all objects to be informed of the change manually, loop through them and call an update method.

To give one useful example: In MyAppSales you can set your main currency so that all amounts get converted into the currency you are used to instead of showing you Japanese Yen. Now what happens if the user chooses a different currency and you have objects like table views, table cells or your own class instances that need to change something whenever the main currency changes. Enter NSNotification and NSNotificationCenter.

Read more

Setting Pre-Compiler Defines

Once you get smart enough to build multiple apps from the same project you probably have to wrap your head around the concept of #define and #ifdef to conditionally activate and deactivate portions of your code. What is somewhat unnerving is that the target build settings change their appearance seemingly uncontrollably and I took a while to work out what triggers this morphing.

The difference lies in what settings you choose in the upper left hand corner of XCode. Only if you select the “Base SDK” setting then the dialog is the most user-friendly.

Base SDK

The build settings for the active target can no be reached by right-click on target, Get Info, build tab. Or alternatively menu option “Project”, “Edit Active Target”. I discovered three places where #define statements can be placed. I am going to show them to you and make a recommendation as well.

Read more

Understanding Autoreleasing

Do you really understand when you need to retain and release some object? And what about autorelease? It took me around 3 months before I grasped when to use autorelease and now it’s second nature to me. But when I needed to explain to a coding buddy I was stumped. I found that my approach was one of feeling rather than knowledge.

So I decided it was time to do an experiment to visualize what happens to the retain count and when an instance is really released. So see what’s happening to our crash test dummy class I made a TestClass that overrides all the important methods so that we get an output to NSLog what is happening.

First I want to demonstrate how the retain counter – present in all Objective-C classes – is used to automatically determine when the instance can be freed from memory. Then I am going to show how to use a factory method which returns an autoreleased instance to make your life easier.

Read more

URL Encoding

When transmitting data in the context of the HTTP protocol you often need to encode text in a way that does not interfere with special characters used in URLs. This is of importance if you want to put unicode characters into an URL query but also for simple things like constructing a body for a HTTP POST request. A form post also takes the form fields and puts them into the form that you know from an URL: field=text&another=more. That’s what the HTML content type “application/x-www-form-urlencoded” means.

The first thing that jumps out of the documentation when looking for a standard function to achieve such “URL Encoding” is stringByAddingPercentEscapesUsingEncoding. So that is what I was using for encoding the password for my iTunes Connect class which drives MyAppSales. And until now this worked without a hitch until customer #113 who was the first to use a plus character in his password. The poor guy ended up locking his iTunes account. Sorry!

It turns out that + is an anachronistic special character substitution for a space. I would have expected for it to be encoded properly by the above mentioned method as %20, but this is not the case.

Read more

Your Own Delegation Protocol

Usage of delegates is prevalent throughout the SDK’s that Apple provides. A delegate is basically a way to tell a standard object “Hey you, if you need some info or want to inform somebody when something happens, talk to this guy. I’m outta here”.

This achieves three things that are an essential skills in programming:

  • you make use of a component without having to do extra work of subclassing it for customization
  • you appoint somebody else to do the work of one-the-fly customizing (the delegate)
  • you can excuse yourself and go to the pool

A good example of usage of delegates is UITableView. This standard class, to be found in UIKit, knows and speaks two protocols: UITableViewDataSource and UITableViewDelegate. As the names suggest the first deals with questions related to the content of the table to be displayed: number of sections, number of rows, headers, individual row cells et cetera. The second delegate, which is also called delegate, deals with interactions on a higher level, like if you tapped on an individual cell. Even though they are called different, both are delegation protocols and if you like you can assign discrete classes to data source and delegate for a table view.

To make this a practical example I will show how to make a class that informs your code when a headset is plugged in or plugged out.

Read more

Determining the Hardware Model

Not all Apple Hardware is created equal. For some apps you need to distinguish between different models of iPhones and iPod Touch. But when you query the model property of UIDevice you don’t get enough information to really know what hardware your app is running on.

With a little bit of googling I found an article on Ars Technica explaining how the pros are getting the kind of information that we regular dev guys dream of having at our fingertips. It basically uses barely documented system calls to get the infamous model string from which you can infer the real device model.

We are talking about sysctlbyname which Apple even documents as a standard C library function call to “get or set system information”.┬áiPhone Dev crack Erica Sadun made this UIDevice category extension. We are inspecting it with awe as we see the magic unfold.

Read more

__MyCompanyName__ Treats Warnings as Errors

I finally figured out three XCode settings that I’ve been dreading to change for quite a while now. See if you didn’t think of changing them yourself as well, but could not be bothered to Google them.

Are you still working for __MyCompanyName__?

If not you might want to change what XCode puts into the standard header which is being generated for all new files.

//  Report.m
//  ASiST
//  Created by Oliver Drobnik on 24.12.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.

There is no UI method of changing this string which is kept in the XCode preferences file. Instead you have to use a command like this.

defaults write PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "";}'

The result being that from now on all code belongs to your company, even if it’s just a company of one.

Read more

Detecting Internet Connectivity

For the latest version of GeoCorder Apple required me to show an alert if there was not internet connection. This is meant to prevent “user confusion” which would occur if the user is expecting something to happen staring at his screen, but nothing happens because of lack of data connectivity.

There is a good example from which you can steal the technique, but it took me some experimenting to find the right mix. For some reason that escapes me the regular connectivity check would only work within the sample, but not in my own code.

Read more