Archive for the 'apple' Category

What to do if XCode won’t add a source code file to a Project

Monday, November 30th, 2009

Sometimes XCode won’t let you add a source code file to a project when you click “Add, Existing Files…” from the “Groups & Files” context menu: you find that the filename is greyed out and cannot be selected.

To work around this, you can drag the file from the Finder to the “Groups & Files” pane.

Teach yourself Cocoa Touch Programming in 24 hours

Monday, October 19th, 2009

A few months ago, Pearson Education contacted me about writing a book about Cocoa Touch. I agreed because although I had a lot of experience on other platforms, I found Cocoa somewhat bewildering when I first encountered it. Most books either assumed a Mac background or really wanted to teach me Object-Oriented programming from the ground up. Often after reading one of them, I had the impression I had learnt something until I tried to apply whatever it was I learned: The problem was that they glossed over how Cocoa really works, and why it was architected in that way.

Cocoa and Objective-C are very powerful tools to build Applications and Graphical Interfaces quickly. This power comes from splitting GUI tasks into a few well-chosen abstractions that reduce the amount of code we have to write. Once understood, these abstractions seem obvious, so most books simply introduce them as the way things are done in Cocoa, leaving beginners bewildered.

Instead I show readers how these abstractions work and how they simplify their code. Because they understand the mechanics of each task, readers will be able to resolve most difficulties on their own quickly, rather than exploring new levels of frustration. To further help, the book is interlaced with debugging techniques. My goal in writing the book was to provide a sound foundation that programmers can use to build functional applications, and feel confident that they can solve the problems they will encounter. Instead of covering the latest API fashions, I concentrate on the APIs and tools you’ll actually use and will need to understand.

The book was published last Thursday, Oct 15 2009, and is available now at the publisher’s. Amazon shows they have yet to receive it (and therefore discount it). It’s in full color!

Understanding the Objective-C Language

In the first five chapters, you’ll build a basic application: a Calculator. You’ll learn how to use the key tools used when developing iPhone applications: Xcode, the debugger and Interface Builder. It also teaches you Objective-C: Objective-C is a thin layer on top of C which adds language support for object-oriented programming. Cocoa uses reference counting for memory management, but the judicious use of auto release pools makes this much easier than you might expect. You’ll also learn about Cocoa Touch’s Foundation classes with provide basic functionality such as Unicode strings, arrays, dictionaries. Practically speaking, by the end of chapter 4 you’ll have written a functioning calculator.

When I first learned Objective-C, I found many bugs intractable because I did not understanding messaging. To help you avoid this pitfall, I explain messaging and how it is implemented in chapter 4. Similarly, it took a while for me to understand auto release pools. They simplify your code, but are often presented as a form of magic you just use. My book is a magic-free zone, clarifying how they work and their limitations up-front. Other Objective-C particularities you’ll learn about are class objects, updating classes on the fly, class clusters, key-value coding and key-value observing. By the end of chapter 5, for most intents and purposes you’ll be an Objective-C expert. Unlike competing books, and Apple’s own documentation, I detail the computational complexity of Objective-C arrays and dictionaries.

User Interface Foundations

The building blocks of user interfaces are views. To build a user interface from views, one simply builds a tree of views (a view hierarchy) which specifies the location and order in which views are drawn. Most interfaces can be built using Interface Builder, a graphical design tool. Unlike competing solutions, Interface Builder does not generate code, but a NIB file that states how to build the view hierarchy. Most books only cover building user interfaces in Interface Builder. As a programmer this left me with a bad taste, as I was relying on some unknown mechanism working behind my back. Debugging was difficult because I had no idea how NIB files were loaded. Instead I show you how to build view hierarchies in code, how NIB files are loaded, and the pitfalls you may encounter.

Views draw themselves using a 2D renderer called Core Graphics (or Quartz). They respond to user interaction dispatched to them by Cocoa. Implementing your own User Interface element (a button) will give you a clear understanding of the ins and outs of user interface elements, and will help you polish your applications with custom user interface elements.

Unlike most user-interface solutions, Cocoa is not architected as a library. That is to say, your application does not just call library functions at will. Cocoa is architected as a framework: it calls your application code when it needs to. This turns out to be a powerful solution, but is disconcerting for programmers coming from other platforms. Because misunderstanding the run loop causes seemingly unrelated bugs I spend entire chapter discussing how run loop is used and how it interacts with other Cocoa sub-systems such as the auto release pool, and the responder chain.

A distinguishing feature of the iPhone’s user interface is how much animation it uses. These animations are created with Core Animation. Core Animation uses the iPhone’s 3D graphics chip and a separate thread to create smooth animations. Understanding Core Animation’s architecture will help you use it and remember its limitations. To further help you I share tips learned developing commercial software. By the end of this chapter, you’ll have implemented your own Cover Flow clone shown above! (animal images courtesy of

Advanced User-Interface Elements

View Controllers reduce the amount of code you must write. Unlike many beginning programmers, you will not confuse them with views because the book clearly differentiates them. An example will solidify your understanding: displaying a Scientific Mode in the calculator when the iPod is rotated.

Tables are an essential component of most user interfaces. The iPhone’s table support is particularly flexible, and I’ll show you how you can customize it to create fast scrolling distinctive tables. In this hour, you’ll build a table based application that lazily loads data from an Internet source: a Twitter application.

Many applications present information on multiple screens. Navigation bars and Tab bars provide a standard way of navigating between the screens. I show you how to build applications using these user interface elements, and show you how the views and view controllers interact.

iPhone OS 3.0 introduces undo/redo functionality. Cocoa Touch solves this problem in a particularly elegant manner. However, it differs substantially from other solutions you may have used. To help you understand it fully, I dedicate an entire chapter to understanding the problems it solves, how it works, and why it was architected in this way.

Accessing the Internet

By providing full internet connectivity and the ability to render most web pages, the iPhone enables whole new classes of applications. To help you build robust applications, chapter 14 discusses how networks work in detail. Working through a real example (adding error handling to the Twitter application) shows you how this is done in practice.

Chapter 15 introduces UIWebView, a versatile User Interface component able to render documents in HTML, PDF, RTF, RTFD, Microsoft Office or iWork formats. You’ll learn how to use Javascript to update HTML formatted pages or parse JSON strings, and how to use new HTML5 features supported by the iPhone.

Saving and Retrieving Data

Cocoa Touch provides four means of saving and retrieving data: application preferences, files, a small sql database, and Core Data. You’ll learn how to add application preferences to the Twitter application, that users can customize in the Settings application. In chapter 17, I discuss when and how to use files or the sqlite database. iPhone OS 3.0 added Core Data which lets you load and save objects to storage transparently by providing a single object definition instead of writing your own classes and your own serialization code. To help you decide whether or not to use Core Data, I discuss its performance characteristics.

Interacting with the World

Only a few years ago, the idea of a tiny device containing your music and video library, finding your location anywhere on the planet, and using accelerometers as a user interface would have seemed like Science Fiction. The iPhone is a remarkable convergence device. Chapters 19 and 20 explains how to use these capabilities in detail.

Chapter 21 shows you how to share data with other applications. Custom URLs let you start other applications. Pasteboards provide cross-application copy and paste functionality. To export data from your device, I show you how to send emails from your application, or run a tiny webserver.

Completing Your Application

Completing your application involves four tasks: debugging it, optimizing it, localizing it and shipping it.

I devote an entire chapter to debugging, showing you not only how to use the debugger gdb, but also dtrace, valgrind, and nib2obj. Sometimes however, bugs are caused by misunderstanding how the frameworks work. Usually other programmers can help you, but sometimes the only solution is to reverse engineer the framework. To help you do this, the chapter ends with a short tutorial teaching you how to do this.

Optimizing your application is another key topic and has its own chapter. You’ll learn how to write your own profiling code, and learn how to use the two iPhone profilers: Shark and Instruments. Because of the iPhone’s memory limitations, it is particularly important to minimize your application’s memory consumption. To further help you, I introduce the Clang static analysis tool which helps you detect memory leaks at compilation time.

The final chapter covers localization and shipping your application. Because the App Store is the only authorized means of distributing applications, and because Apple can reject your application once you submit it for publication, developing for the iPhone is somewhat risky. To help you mitigate this risk, I analyze the types of applications Apple has refused to distribute on the AppStore, and provide some simple guidelines to follow.

Additional materials

If you haven’t programmed C in a while, I provide Appendix A to quickly refresh your memory of the salient points.

While developing for the iPhone you may encounter a number of issues due to Apple’s development tools. To help you resolve these issues quickly, Appendix B devotes 18 pages to debugging these issues.

Appendix C lists resources I have found useful when developing iPhone applications.

Appendix D includes a number of advanced topics of interest to expert programmers: how Cocoa starts your application, a deeper discussion of exceptions, and how and when to use threads.

P.A. Semiconductor to focus on the iPhone

Wednesday, June 11th, 2008

The New York Times reports that P.A. Semicondutor will be making systems on a chip for the iPhone platform. So I got this wrong.

Two Iphone CPU Architectures?

Thursday, May 15th, 2008

Intel’s managing director responsible for Germany has apparently revealed that there will be 2 iPhones, one based on an ARM processor and the other on Intel’s Atom processor.

Perhaps this explains this WWDC picture:

(image courtesy of computer world)

Apple buys P.A. Semiconductor

Wednesday, April 23rd, 2008

At the time Macs started using x86 cores, an alternative to stay PowerPC was to use P.A. Semiconductor’s chip. At the time (2005), it would have been a risky move as P.A. Semiconductor didn’t yet have working silicon. Today however, Apple bought P.A. Semiconductor.

A Desktop Part

What’s interesting is that P.A. Semiconductor’s part, the PA6T, is a desktop part, not something that would fit into an I-Phone.

Wattage: The IPhone’s CPU uses quarter of a watt (it’s either a Samsung S3C6400 or a close derivative). The PA6T uses between 5 and 13 watts, and in the worst case 25W. While this is lower than an Intel desktop chip, it would take too large a battery to fit in a phone.

Memory Bus: The PA6T has two CPUs, each with its own DDR2 bus. That’s a lot of pins, and a lot of memory chips — not something that fits into a phone.

I/O: 8 PCIe controllers, 2×10Gb ethernet controllers, 4×1Gb ethernet controllers. None of this is needed by a phone.

CPU: Two 2GHz PPCs for the 5-13 Watt Core… Apparently future versions will run at 2.5 Ghz, and provide up to 8 cores on a die. Again overkill for a phone.


Although it’s possible that P.A. Semiconductor has another IPhone specific chip in the works, my guess is that the next MacBook Air will use this part. The MacBook Air uses a 20W part, and a 37W hour battery. As a rough calculation, 17W for the system, 20W for the CPU becomes 22W with a 5W CPU, and 20W for a system with a solid state drive. That gives you almost 10 hours of battery life.

This is another step in Apple’s strategy of owning the whole system, letting it chart its own course, although I expect it to continue using commodity x86 chips for lower end systems. For developers, it means we’ll be making FAT binaries for some time yet, even if it does mean more work.

Apple tries 10 different UI designs for each new Feature

Tuesday, March 18th, 2008

Apple designers try 10 different mockups for each new feature. That explains a lot.

A better Software Update mechanism

Monday, August 13th, 2007

I’m using Sparkle to update Find It! Keep It!. It’s a state of the art desktop application update solution. It would however like to see some improvements.

What I don’t like about the current solution

Don’t interrupt me!

Sparkle asks whether you want to update the app when you start it. When someone opens his mouth, it’s not a good time to ask him something: he’s about to say something. Starting an app is the same.

Sparkle can also ask you whether you want to update the app when it’s running. That’s just plain rude: it could be interrupting the user’s thought process, especially as, IIRC, it causes the application’s icon to bounce if you’re using another app.

How am I supposed to know?

There is no way the user can know whether an upgrade will improve his experience, or waste time as he tries to recover his mistake. Asking can only make people feel stupid. Web Applications don’t ask. You get the upgrade, and you may not even notice.

Automatically upgrading software on other people’s computers could be a support nightmare, so it must be very easy to go back to where you were. Sparkle currently doesn’t provide a downgrade path.

Don’t make me wait!

Sparkle downloads the whole app on each update. For people on slower connections it’s noticeable and irritating. It would be better if the user didn’t notice it.

Overall impact

About 60% of my users seem to have disabled Sparkle. That’s a lot given that during the beta-test cycle, upgrades are not optional.

A solution

Transparent upgrades

Updates are small bidirectional binary diffs (created using bsdiff for example) that are downloaded in the background. To minimize user visible lag, they could be downloaded in small sections.

By default upgrades are applied silently at application startup, but the user can require a confirmation step using the Application’s Preference Panel.

In built version selector

The Application’s Preference Panel easily lets users switch back to an older version. It shows the version information for each version that Sparkle currently only shows when checking for a new update. Because each downloaded upgrade is a bidirectional diff, one can always downgrade.

Downgrade Appcasts

The Appcast Sparkle currently uses to inform the Application that an upgrade is available is enhanced so that a developer can correct an “Oops” by retracting a faulty upgrade. Adding this to the appcast protocol would be easy.


A small unchangeable harness is used to launch and monitor the App. If the app crashes on startup before the user could access the preference panel, it retracts any recent unproven upgrades and sends crash logs to the developer, after asking the user. Using a process to monitor another is extensively used for high reliability Telecom Systems: nine nines reliability (that’s 30ms downtime a year).

Growl and Email

If Growl is installed, it can be used to inform the user of the upgrade.

Generally however I think email is the most appropriate forum to inform the user of updates: one is more inclined to read information if one is not busy doing something else. Notice that even users who worry about providing their email addresses to the software developer can be catered for: the app itself can email the user with the information currently in the appcast when an upgrade has been downloaded.

Why I’m hoping Sparkle will implement this

I’m hoping the current Sparkle improvement discussion will consider and adopt this idea: Sparkle is already a standard, with tools like AppFresh supporting it.

IPhone apps: they weren’t kidding

Friday, June 22nd, 2007

The new WebKit inspector may be the demonstration vehicle that convinced Apple that AJAX UIs could work, despite many people’s concerns.

Very few applications so far use HTML for rendering. Find It! Keep It!’s Database view is rendered as HTML and uses Javascript within the application. NetNewsWire’s Combined View is also implemented in this way. WebKit Inspector goes one step further, by emulating what looks to be a Cocoa app in Javascript extremely effectively. Like Find It! Keep It!, it calls native functions to perform operations that would otherwise be impossible with Javascript. However it also uses the canvas element to make its bar graphs. Its only flaw is that it does not scale correctly when you press Command + to make the text bigger.

Why Safari will double its market share

Tuesday, June 12th, 2007

Although most people don’t change default browser, people who use Safari at home will be tempted to use it at work on the Windows box they’re saddled with… Particularly because of the more familiar font rendering. As a result the number of Safari users will appear to double (different IP addresses, cookies, etc). This sudden increase will be hailed as remarkable, causing other people to try out the new kid on the block. As pointed out by John Gruber, Google will pay for the port by sharing ad-revenue from Safari’s search box. Apple can also expect to leverage the Itunes halo effect, especially if it bundles Safari with Itunes.

Now a wild guess: Just as Itunes is the interface for the Ipod, Safari will be part of the required interface for the iPhone, letting you share bookmarks with your phone among other things (Bonjour is installed by Safari for windows).

Apple postpones Leopard

Thursday, April 12th, 2007

From the horse’s mouth. This must be pretty annoying for the people who are making Leopard only apps. I guess they’ll take a vacation.

Although John Gruber says he’s not surprised, Gus Mueller is in shock