365Cocoa – Day 12: Custom drawing
Mar 11th
As I have neglected to post the second in my series on Cocoa drawing I’d like to point you to 365Cocoa for their next few posts starting with the one linked below. It sounds like they are going to go down the path I intended (and will probably do it better than I could have). Enjoy!
Indie+Relief an Amazing Success!
Jan 22nd
Many congrats to everyone who participated in Indie+Relief. Over $143,872 was raised for the people of Haiti through various charities!
The Awesomeness That Is Bodega
Jan 17th
Let’s get one thing out of the way first thing: I’m a big fan of Bodega and have been since I heard about it firsthand from the guys at centrix.ca during WWDC 2009.
Bodega is a free Mac application that helps you discover and download Mac applications. It is built in the spirit of great software sites like MacUpdate, but takes the experience and puts a great looking Cocoa wrapper around it. Make no mistake, this project is a big undertaking with a lot of moving parts and a lot of coordination between those parts. Yet with steady progress and determination these guys are really crafting something cool.
The impetus for this particular blog post is to gush about what I think is Bodega’s killer feature: keeping track of and installing updates for the applications installed on your computer. I really like Andy Matuschack’s Sparkle framework, but one of the necessary evils of that framework is that it doesn’t notify you that there is an update for a particular app until you run that app. When I launch an application it is done with the intention of performing a task immediately, but if there’s an update for that particular app I am yanked out of my workflow to deal with the question of updating the app first. Yes, I can continue to work, but in the back of my mind I’m thinking about how I don’t have the latest version of the software that I am currently using, and it bugs me.
Enter Bodega. Once every few weeks I will launch Bodega and click on the Applications section in its sidebar which presents me with a list of all the software currently installed on my system*. The killer feature is that Bodega scans all of the Sparkle feeds in these apps and will tell me if there’s an update for any of them. With two clicks (one to download and one to install) I can download and install any of the updates that I want. This simple process greatly reduces the chance that the next time I launch an app I will be presented with the option to download and install an update, Bodega has already taken care of it for me.
If you haven’t downloaded Bodega yet I highly recommend that you do and take it for a spin. If you’re a developer and you haven’t submitted your software to Bodega, I highly recommend that you do that too.
* Bodega is opt-in for developers, so if a developer hasn’t submitted their software to the Bodega site it won’t know about that software on your computer.
Indie+Relief
Jan 17th
For those that may not have heard, Justin Williams has organized a great effort to provide financial relief to the people of Haiti. Set to take place throughout the day on January 20th, all the proceeds from the sale of participating software will be donated to Haiti. We here at No Thirst Software are participating with sales of MoneyWell and Debt Quencher. For a full list of participating software please check out the Indie+Relief website.
It’s a great cause and I know that I will be checking the list for software that’s on my “to buy” list. I suggest that you do the same.
iPhone Memory Debugging with NSZombie and Instruments
Jan 15th
NSZombie is easily one of the most valuable tools in any Cocoa developer’s toolbox. Check out Mark’s blog for a great introduction on using NSZombie from within Instruments:
iPhone Memory Debugging with NSZombie and Instruments | markjnet.
Thanks to Jeff LaMarche for directing me to Mark’s blog in the first place.
A Successful git Branching Model
Jan 14th
Being a bit of a git newbie I found the following post very useful in setting up our DVCS model. The graphic that leads of the post is a bit intimidating but it all comes together very well after reading through the post. I highly recommend this post to anyone that uses git in a team environment.
Friday Q&A: NSNotificationQueue
Jan 8th
Mike Ash exposes another Cocoa gem this week with his explanation of NSNotificationQueue.
You can read about it here: http://www.mikeash.com/?page=pyblog/friday-qa-2010-01-08-nsnotificationqueue.html
Custom Drawing Using drawRect, Part 1
Dec 18th
One of the more advanced techniques for creating custom user interfaces on the Mac is the use of NSView’s drawRect method. Many answers to questions on StackOverflow and Apple’s mailing lists include recommendations to “just override drawRect and do the drawing yourself”. Some folks see this recommendation and their eyes glaze over, thinking that it’s too advanced of a technique for them to wrap their heads around. Over the next few days I’m going to go over some basic techniques that can yield powerful results.
Let’s start by setting up the Xcode project that will be the basis of the rest of these posts.
- Open Xcode and create a new Cocoa Application project called DrawingSample.
- Create a new NSView subclass called CustomDrawingView.
- Open MainMenu.xib, add a new Custom View to the Main Window, set its class to be CustomDrawingView, and set it’s autosizing flags as seen here:
Save and Quit Interface Builder and switch back to Xcode. Open CustomDrawingView.m, it should look like so:
@implementation CustomDrawingView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self; } - (void)drawRect:(NSRect)dirtyRect { // Drawing code here. } @end
We’re going to start (and finish) today with just a simple concept and some basic drawing that will set the stage for the future posts. All drawing in Cocoa is done by first setting up the environment in which you want to draw, and then doing the actual drawing. For instance, if we want to draw a blue box, we first have to setup the color blue, define the bounds of the box, and then draw it. In this case we are using the NSRect that is passed to the drawRect method as the box we want to draw, and we setup the color blue by calling [[NSColor blueColor] set]. We then use the convenience method NSRectFill to fill the dirtyRect with the color blue. Notice that we didn’t pass the color to NSRectFill, we set it, and from then on anything we draw will be blue until we change the color.
You can think of drawing in Cocoa much the same way as you would think of painting with a brush. You dip your brush in a certain paint color, paint the shape you want to paint, and then dip your brush in a new color and paint some more.
- (void)drawRect:(NSRect)dirtyRect { [[NSColor blueColor] set]; NSRectFill(dirtyRect); }
The preceding code, when run, will generate a view that looks like this:
Now, this may not look like much, but in future posts we will build on these concepts and, hopefully, by the end have drawn some pretty cool and useful things.
Bottom Bars in Interface Builder
Dec 18th
Dave posts a handy tip for those that may not know that there’s a non-code way of setting up the bottom bars on your windows. Check it out here: Bottom Bars in Interface Builder – Dave Dribin’s Blog.
iLife 09 Won't Install When the iPhone Simulator Is Running
Oct 20th
This post is a bit of a deviation from my usual posts, but it falls into the something-that-bit-me-that-might-bite-you-too-so-here’s-how-to-fix-it category.
I recently upgraded to Snow Leopard by doing an erase and install. After getting everything back up and running I realized that I still needed to install iLife. I popped in disc 2 of my MacBook Pro install DVDs and proceeded to open the Install Bundled Software installer. After customizing my options I hit the install button and waited. And waited. And waited. The installer never got past the Preparing Bundled Applications stage.
I checked out the console and saw a series of messages that ended with:
10/17/09 4:23:10 PM Installer[8135] *** -[NSMachPort handlePortMessage:]: dropping incoming DO message because the connection or ports are invalid
I decided to try a restart to see if that would fix my problem but shortly after restarting and before I could try my install again I cranked open Xcode and the iPhone simulator to check out something in my latest project. Remember this part of the story, because it’s important.
A few days later I remembered that I still needed to install iLife and grabbed my MacBook Pro installation discs again. I kicked off the installation and instantly hit the same issue. The installer never got past the Preparing Application Bundles stage. I opened Console.app again and saw the same message:
10/20/09 7:39:40 PM Installer[9025] *** -[NSMachPort handlePortMessage:]: dropping incoming DO message because the connection or ports are invalid
I decided to turn to Google and happily came across this gem of a radar filing: Installer hanging for any installer after uptime of X hours. If you read through the comments on that bug you will find that the installer hang isn’t based on uptime, but rather on the iPhone Simulator running. I had been working on an iPhone project both times I tried to install iLife and it was causing the installer to hang (for some inexplicable reason).
I quit the simulator and Installer.app immediately stopped trying to prepare the bundled applications and displayed an error. I quit the installer, tried again, and voila, everything worked as it should.


