5 years

Today it’s the 5th anniversary of the article that would eventually become the first post of this blog. I was leaving Buenos Aires, again, and I wrote that on my old G3 iBook in the airport of Ezeiza, right before boarding. That trip was very important, for many reasons that don’t fit on a single blog entry.

Since then, I met Claudia, we got married, I finished my master degree and started my own company. Even hernún came to Switzerland! I’ve moved from talking about .NET to giving interviews about the iPhone. I’ve published as much text and code as I could, but most importantly, I kept on creating things.

Thanks to all of you, for your comments, your support, your ideas, your code, your critics. I’ve really learnt a lot during these years, and I hope my ramblings will be useful to you in the future as well.

Cheers! Salud! Santé!

Using Multiple Twitter Clients from your iPhone Application

I love playing with iPhone URL schemes. And if you ask me, just like for Mail.app and Safari, I think there should be a “default” Twitter client URL scheme in the iPhone, with an interface in the Settings application allowing you to set the application that you prefer to tweet. Alas, this is not the case, and each application must manually allow users to select their preferred client, from a list of known ones.

Having documented iPhone URL schemes for TwitterFon, Twitterrific, Tweetie and Twittelator, I’ve created a project, available in Github, called TwitThis, which helps users choose their preferred Twitter client, and makes it easy to remember the user choice, and to launch the associated application with a single command:

TwitThis

This application has the following features:

  • The class TwitterClientManager loads a list list of supported Twitter clients is loaded from a plist file, which can be extended to support more clients in the future;
  • Each Twitter client is represented by an instance of the TwitterClient class;
  • The user can choose his preferred Twitter client at any time, and launch the application by a simple touch; the TwitterClientManager class stores the selected value in the user settings.

If you have to support several different Twitter clients, feel free to use these classes in your own project! The project, as usual, is available in Github with a liberal BSD license. Enjoy! I’d love to hear your comments below.

Code Organization in Xcode Projects

Xcode does not impose any structure to your source code tree. This is both cool and useful to quickly throw a couple of lines for a prototype, but in my experience, this approach does not scale. More often than not, without any hygiene, your project can become a mess. Just using Xcode defaults, after a while your resources will sit beside your .xcodeproj file, all the project classes will be thrown together in the Classes folder, and if you have a relatively large project, this approach makes finding individual files painful.

Of course, Xcode provides “Groups” to organize your source code, but the idea is to be able to quickly identify the different kind of files that make up your Xcode project, either for Mac or for the iPhone, without having to open the Xcode project file. This means having both a folder structure, and an internal source code file structure. All of this will help you maintain your project in the future, which means cheaper costs, and less time spent looking for bugs.

All of this is also particularly useful when browsing projects via Google Code, Github or any other kind of file view of source code repositories. If your code is organized in a nice folder structure, it is easier to explore than if all the files sit in the same folder.

In this post I will enumerate some best practices that I use in all of my projects. Continue reading

Objective-C Compiler Warnings

A recent comment by Joe D’Andrea in a previous post reminded me about the importance of removing compiler warnings in Xcode projects. Most importantly, it reminded me of a conversation with a fellow developer a couple of weeks ago, in which he told me that he was surprised to see that my projects compiled all the time without warnings. Not a single one. Nada. And that I took the time to remove them before checking code into source control.

He actually didn’t know you could remove all compiler warnings; he thought Objective-C was the land of compiler warnings. This situation, I think, is far from exceptional, and due mostly to cultural and technical reasons.

It is my opinion, that removing compiler warnings is basic project hygiene, like writing unit tests, or using the Clang Static Analyzer. I will explain in this post some techniques I use to remove warnings in my Objective-C code.

warnings2 Continue reading

WordPress 2.8 and the get_link() error in line 647 of dashboard.php

Wow, that’s a long title, but it should drive people with this problem right here. If you have upgraded your WordPress installation to 2.8, you might have encountered a nasty error in your Dashboard, which says something about a

Fatal error: Call to a member function on a non-object in /home/user/www/wp-admin/includes/dashboard.php on line 647

This has been reported in the WordPress site but no fix has been provided. I found elsewhere a possible fix, but in my case, the new URL would not be saved at all, and the problem would persist.

I fixed it using a good old method, enabled by Open Source™: editing the code directly; I’m posting it here for those of you who would like to do it, until 2.8.1 is released:

[source:php:firstline(646)] $author = $item->get_author(); if ($author == NULL) { $site_link = “”; $publisher = “Someone”; } else { $site_link = esc_url(strip_tags($author->get_link())); if ( !$publisher = esc_html(strip_tags($author->get_name()))) $publisher = __(‘Somebody’); if ($site_link) $publisher = “$publisher“; else $publisher = “$publisher“; } [/source]

OpenGL ES 2.0 on iPhone OS 3.0

Now that the NDA on the iPhone OS 3.0 SDK has been lifted (which happened much faster than what I thought it would take!) here’s my first contribution to the world of iPhone OS 3.0 open source code: sample code about how to use OpenGL ES 2.0 on the iPhone 3GS, something I announced in Twitter last week.

As you might know by now, one of the biggest enhancements (and yet, one of the most obscure) of the newly released iPhone 3GS is the new GPU chipset, which allows developers to create applications using Open GL ES 2.0 (together with Open GL 1.1, which was already available in the first two iterations of the iPhone). This is a major advance, invisible to the end user, which, coupled with the unprecedented performance boost of the iPhone 3GS, opens up the possibility to developers to create applications with new textures and effects, yet unforeseen on this platform.

Given that Xcode does not (yet) bring an Xcode template to play with, and that the OpenGL ES 2.0 Programming Guide book, by Aaftab Munshi, Dan Ginsburg and Dave Shreiner does not (obviously) bring iPhone examples, I have created a project in Github where I will be publishing the code samples in the book, as I progress in the lecture, ordered by chapter, ready to compile and play with.

Enjoy! I’m happy not having to use the word “[REDACTED]“ any more now (there’s the other OS, the bigger cat, but, oh well…)

Update, 2009-06-24: I just found this blog post by the folks of Black Pixel (Daniel Pasco‘s company) with benchmarks about OpenGL ES on the iPhone 3GS… and the first line says it all:

Holy crap, this thing is fast

Update, 2009-06-24: Jeff LaMarche just announced that the authors of the book have published iPhone – compatible code in the book website! That effectively renders this project useless :))

Random Quotes on Business and Software

A Cooperative Organization:

(…) Gore has been a team-based, flat lattice organization that fosters personal initiative. There are no traditional organizational charts, no chains of command, nor predetermined channels of communication. Instead, we communicate directly with each other and are accountable to fellow members of our multi-disciplined teams. We encourage hands-on innovation, involving those closest to a project in decision making. Teams organize around opportunities and leaders emerge.

Continue reading

NIBs or code? Why not both? Here’s nib2objc.

(Somehow this project seems to me so simple, that I’m sure someone has done this before. Anyway). This is my feeble attempt to bring an answer to the eternal dichotomy between those arguing about the relative benefits of creating user interfaces via Interface Builder or via pure Objective-C code: let me introduce nib2objc.

Unbeknown to most of us, the ibtool utility bundled with Interface Builder and Xcode allows us to inspect the contents of NIB files (or XIBs, for that matter) and get from them nice property lists XML streams, which I transform in NSDictionary instances, which I loop over and over util I get something that looks like this:

[source:c] UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)]; view6.frame = CGRectMake(0.0, 0.0, 320.0, 460.0); view6.alpha = 1.000; view6.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; view6.backgroundColor = [UIColor colorWithWhite:0.750 alpha:1.000]; view6.clearsContextBeforeDrawing = NO; // …

UIButton *view9 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; view9.frame = CGRectMake(167.0, 65.0, 72.0, 37.0); view9.adjustsImageWhenDisabled = YES; view9.adjustsImageWhenHighlighted = YES; view9.alpha = 1.000; view9.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; view9.clearsContextBeforeDrawing = NO; view9.clipsToBounds = NO; view9.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // … [view9 setTitleShadowColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateSelected];

// … [view6 addSubview:view9]; // … [/source]

Using this tool, I can now use IB for design, and then generate the code for those designs, in case I prefer to use a code-only approach (usually for UITableViewCells, as I explained before). For the moment it only works with UIKit classes, but I don’t think it might be a problem to extend it to AppKit classes as well.

I hope this project is useful to all of you! As usual, open source, public domain and on Github.

Update, 2009-04-09: This project has been featured in an article in Ars Technica by Erica Sadun!

That iPhone Keypad

Finishing my series of copied flattered UIs, like that Facebook thingy or that Twitterriffic gadget, here’s Apple’s own iPhone keyboard, in a really sloppy implementation that has been blatantly and horribly copied, with awful sounds that pop when you tap the numbers and such. The Fring iPhone application uses a similar keyboard, but with a different color set.

This code, apart from showing the keyboard and playing those sounds, it doesn’t do anything else, even if a simple “tel:” URL call might suffice to turn it into a real dialer. As usual the code is on Github; feel free to play with it, extend it, and do what you want.

Asynchronous Loading of Images in a UITableView

This is one of the most common scenarios for network- + UITableView-based applications; a UITableView instance whose contents come from the network; not only the text, but also the images! Somehow we all want to reproduce the behavior of the App Store (or of the iTunes) iPhone application, where the icons (or covers) of the apps (or songs) are downloaded one by one, as you scroll the table, without crashing nor opening 1000 simultaneous network connections.

My solution (there might be many others!) consists in the following key elements:

  • Avoid loading images right after the RSS feed is parsed; instead, use the -tableView:willDisplayCell:forRowAtIndexPath: delegate method in the controller to trigger the loading of the image for cells that become visible; this ensures that only the cells that are visible will receive the order to load images from the network;
  • Use a “model” class for each element of the table, and make the custom UITableViewCell subclass a delegate of this model object; then, the model object is responsible of loading its own image, and will tell the UITableViewCell when done;
  • Use the ASIHTTPRequest framework, with a shared download queue in the application delegate, so that all of the requests are properly queued, and network resources are properly used;
  • Show feedback to the user with scrolling wheels whenever and wherever appropriate;
  • Use the Reachability class from Apple’s own sample code to see if we’re really connected to Flickr, and otherwise show an error to the user.

I have created a sample project, as usual in Github, where I gather RSS data from Flickr’s public feed, and then I download synchronously the preview images of the feed. I even included a very simple Core Animation effect which reminds me of how the iTunes iPhone application allows us to hear previews of the songs we want to buy (the image flips and shows another view “behind”).

Feel free to contribute, fork, enjoy, read, use in your own projects, as you want. As I said, there might be many other (and most probably better) approaches to do this, so feel free to leave your comments below, as usual.