Del.icio.us to WordPress

I’ve just uploaded a new project on Github called delicious_wp: it’s a small Ruby script that simply fetches the items stored in del.icio.us the previous week and creates a blog post with them. You can set up a small cron job to execute this script every week, which is what I’ve done for this blog :) I know del.icio.us has a similar feature integrated, but it executes daily, instead of weekly, which is what I wanted.

To use it, just clone the repository, copy the config.yaml.sample file as config.yaml and edit its values inside. Run the script and voilĂ ! A new blog post entry with your del.icio.us bookmarks.

The script can also be helpful to those wondering how to use the XML-RPC interface of WordPress from a Ruby script, or how to use the Net::HTTP library to consume a REST API.

[source:ruby] def get_delicious_bookmarks # Connect to delicious and get updates http = Net::HTTP.new(DELICIOUS_SERVER, DELICIOUS_PORT) http.use_ssl = true req = Net::HTTP::Get.new(DELICIOUS_DATES_PATH) req.add_field(“User-Agent”, DELICIOUS_USER_AGENT) req.basic_auth username, password response = http.request(req) results = response.body [/source]

[source:ruby] def post_to_wordpress(title, text) entry = { :title => title, :description => text } # Connect to WordPress using the XML-RPC interface blog = XMLRPC::Client.new(server, path, port) blog.call(“metaWeblog.newPost”, blogid, username, password, entry, true) [/source]

Enjoy! As usual, the code is released with a BSD license.

Thoughts about Google’s “Go” Programming Language

Historically, we can distinguish really big software companies for providing, at least, four major kinds of products: an operating system (sometimes open sourced at a certain level), a web browser (with various degrees of standard compliance), a suite of office applications (slightly compatible with everyone else’s), and a programming language with curly brackets (generally incompatible with everything else). In that particular order, we have:

  • Microsoft: Windows, Internet Explorer, Microsoft Office, and C#.
  • Sun: Solaris, HotJava (sic), StarOffice, and Java.
  • Apple: Mac OS X, Safari, iWork, and Objective-C.
  • Google: Chrome OS, Chrome, Google Docs, and… Go.

Precisely, Go was the last piece that Google had to create in order to fit into the framework above. And it did, with a bright team including Ken Thompson (of Unix and C fame) and Rob Pike (of Plan 9 and UTF-8 fame). With names like that, and with Google’s own funding and infrastructure, it is normal that the media went into a hype frenzy yesterday.

bumper480x270

I think, however, that Google’s engineers got tired of what the current and upcoming versions of their “official” programming languages (Java 7, C++0x and Python 3.0) had to offer, and simply came up with a programming language that fits better their needs and expectations. As one of the slides of the TechTalk says, with current languages “You can be productive or safe, not both.”

Features like built-in support for concurrency or garbage collection hide the real true feature behind the language: faster build times with static typing support. This is important for Google from a software economy point of view: they want more productivity from their developers, or, in other words, more bang for their buck, all together with verifiable quality and speed of execution. Go seems to be designed to deliver in these areas. However, Rob Pike is careful to say that the language is experimental, so time will tell if their efforts were worth it.

In any case, it is worth noting that there was a previous programming language called Go! (whose author even wrote a book about it), and after an InformationWeek article revealed this, a petition has started in the Go bug tracking, asking Google to change the name of the language, all in the name of Google’s own “Don’t be evil” motto.

Discovering a Hidden iPhone URL Scheme

As an iPhone developer, one of the simplest and easiest mechanisms you have to interact with other applications is through the use of iPhone URL Schemes. These are so important that I’ve created a wiki page where I keep track of those I come across, including code samples that help me exchange data with them.

xcode

However, not all editors document the URL schemes they support in their apps, and this blocks reuse and collaboration. I recently came into such a problem, trying to use TwitterFon from my own apps, to post messages to Twitter. The TwitterFon site only specifies the following iPhone URL scheme:

twitterfon:///post?this%20is%20a%20test

The problem is, this URL scheme does not perform an URL-decoding on the message parameter, which means that a phrase like “this is a test” will appear in TwitterFon URL-encoded, that is, as “this%20is%20a%20test”. Clearly not acceptable.

However, thanks to Ashley Mills, I learnt that the USA Today iPhone app is able to use TwitterFon to share articles via Twitter, and does this properly, without URL-encoded characters. How do they do that? Obviously, they are using an URL scheme exported by TwitterFon, but not documented anywhere (*). I finally discovered that the URL scheme sought is the following (“message” instead of “post”!):

twitterfon:///message?some%20text%20here

This is how I found out: I impersonated TwitterFon in my own iPhone with an ad-hoc app created in Xcode, that shows me the URL used by USA Today to launch TwitterFon. Continue reading

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

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.

That Twitterriffic editor

They say imitation is the best form of flattery. Well, here’s another attempt at doing that, after my “attack” on the Facebook iPhone app (decidedly I’m on a somewhat copying mood lately).

I use Twitterriffic a lot, both on the iPhone and on my Mac, and particularly in the iPhone version, I’ve always liked the little editor for tweets. It grows and shrinks as you type, it appears and disappears following the keyboard, and it provides a standard toolbar with many useful buttons (Actually, I wish the Mac version would have a similar text entry box, which would grow bigger as I type; it’s probably the only complaint I have about it!)

Well, here’s my own attempt at doing something similar, and after 1 hour of work, the result is published, ready for you to enjoy at Github. As usual, no strings attached, pure public domain stuff, so use it and play with it as you wish.

Going Github

This is something I’ve been looking forward to do for some time. After praising git back in 2007, now I’m moving many of my personal projects to Github, which has an absolutely brilliant service! For the moment I’m using the free account, but I will most probably switch to a paid account soon. The only thing it lacks, in my opinion, is a bug & issue tracker as you have in Google Code repositories, but other than that, it’s simply perfect.

So feel free to check out the projects I’ve moved there, and of course, to fork them and enjoy the code as you see fit.