Used iPhones are still worth big money!

A month or so ago I finally broke down and upgraded our iPhones to 3G, and sold our old ones on Ebay. It worked out well.

Bought – 2 16gb iPhone 3Gs, $300 each + tax & misc AT&T fees.
Sold – 2 8gb first gen iPhones for $330 and $335 – about $20 each for Ebay/Paypal fees and shipping.

So the initial purchase is pretty well canceled out. The new service plan is eligible for corporate discounts, so hopefully my discount will cancel out the more costly monthly bill.

Use NSUserDefaults to persist data between app runs

On the iPhone you generally want your application to start looking like it did when it was last run to give the user the impression your app is ready to pick up where it left off. NSUserDefaults provides an easy way to store your application’s settings between runs. Simply create an instance of NSUserDefaults and give it the key/value pairs you want it to save.

Storing an integer

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger: myIntValue forKey: @"intValueKey"];
[prefs synchronize]; // writes modifications to disk

Reading it back in

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
int myIntValue = [prefs integerForKey: @"intValueKey"];

or more simply

int myIntValue = [[NSUserDefaults standardUserDefaults] integerForKey: @"intValueKey"];

Of course, this would be pretty useless if it only worked for integers. The same basic syntax also works for setObject/objectForKey and some others. Check the documentation for details. I have not tried with complicated compound objects yet, but NSUserDefaults so far I have had success with strings and simple arrays.


I just learned about NSNotificationCenter. Any class in a program can send a notification message to the notification center, and all classes can listen to the center and respond to messages that apply to it. It’s kind of like a callback, but really not.

This is part of my code for implementing a custom keyboard in my iPhone app.

To send a notification named “keyboardDone” to all objects in your program.

[[NSNotificationCenter defaultCenter] postNotificationName: @"keyboardDone" object: nil];

To listen for the “keyboardDone” notification and call the “keyboardDoneObserver:” method when it’s received this can be added to your class’ init code.

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyboardDoneObserver:) name: @"keyboardDone" object: nil];

And this is the “keyboardDoneObserver:” method that gets called when notified.

-(void)keyboardDoneObserver: (NSNotification*)notification {
     // do work here

Adding a code beautifier script to Xcode

Xcode’s re-indent command is pretty weak compared to the code reformatting built into other IDEs. It fixes the indentation at the beginning on each line, which makes a huge difference in code readability, but I don’t think it does anything else. In Eclipse and VS I’ve gotten used to being fairly lazy about being consistent with my spacing elsewhere and relying on their built in reformatting to clean it up for me. I wanted to change the way Xcode reformats code, since it’s easier to change tools than to break bad habits.

I spent some time searching for command line code beautifiers that support Objective C and only really found one. Uncrustify is open source, which is pretty fortunate since they don’t have an OS X binary download of the current version. The source download includes the needed project files for Xcode, so compiling your own is easy enough. Just open uncrustify.xcodeproj and compile. You could also download another project, Universal Indent GUI which includes an already compiled Uncrustify executable along with a GUI front end.

After I compiled the executable, I copied it and the defaults.cfg file to a new directory and created a couple user scripts in Xcode to use it. The code for both scripts is identical, just adjust it for the correct paths to the files.

echo -n "%%%{PBXSelection}%%%"
/path/to/uncrustify -q -c /path/to/defaults.cfg
echo -n "%%%{PBXSelection}%%%"

The “Reformat all” script will run Uncrustify on the entire document.

The “Reformat selection” is the same except “Input” is set to “Selection” and “Output” is set to “Replace Selection”.

The defaults.cfg does pretty well with obj-c but isn’t perfect. At least it’s consistent. One of these days I’ll find a better config for it it try fixing it myself.

Activating iPhones. Two iPhones – one family plan

Over the weekend my wife and I upgraded our cell phones.
We’ve had a family plan with T-Mobile for several years and had been fairly happy with it. Especially the price, both of our phones combined only cost us about $50/month. Lately we’ve both been having problems with the service, which makes me think T-Mobile is having network problems as opposed to one of us having a bad handset. For the last couple months we would make or receive calls and we would be able to hear the person on the other end but they would not be able to hear us, and like I said, it happened with both of our phones. Friday was the last straw, as I called my wife 4 times and we had problems each time.

So, Sunday we went to the local AT&T store. I would have rather gone to the Apple store but AT&T was closer and gave us an opportunity to look at other phones. Anyway, we bought 2 iPhones, which we activated once we got home. Activation was easy, but there was a little confusion on signing up for the family plan. One of the options in iTunes is to sign up 2 or more phones on a new family plan account. I assumed after I set up the first phone it would prompt me to set up additional phones, but this was wrong. After the first phone was activated there was nothing to tell me what the next step should be, so I plugged in the 2nd phone and selected the family plan option again. About half way through I realized it was going to create another separate account instead of adding the phone to the account I’d just created, so I started over and used the existing customer option to add a phone. I guess I was only a new customer for the first phone, which I suppose is true, but since the option I picked originally said activate multiple phones I assumed it would prompt me for multiple phones. In any case, it only took about 20 minutes to activate my first phone and transfer the number from my old one, and about a half hour for the second including time I wasted trying to figure out what I was doing wrong.

I have not called T-Mobile to cancel my old account. I had logged into their website to view my account information and find my account number prior to moving the phone numbers. After the new phones were fully activated I tried to log into the old account again to look for instructions on cancelling, and could not log in. The web page told me the numbers had been cancelled, so I’m thinking the account closed automatically.

AK Octane chair

Last week I purchased my latest cheap office chair. It’s an AK Designs Octane chair purchased from Best Buy. My experience is that any of the sub-$200 chairs purchased from retail stores will last about two years before the mechanism that allows the chair to rock and turn will wear out to the point that it loudly squeaks when you use it. At $150, I fully expect this chair to be no different.

The good

This chair is just really comfortable. The sides of the back wrap around you so it feels similar to a sports car seat designed to hold you in place while cornering. Also, the back has a bit of a lumbar bump built into it that feels great on the back.

The armrests adjust up and down, side to side, and pivot. I’m not sure if this is useful, or just fun to play with. Either way I like it.

The chair has big, roller blade style, wheels that roll smoothly on a variety of surfaces. They are even pretty good on carpet.

The blue and black color scheme is a perfect match for my blue walled and black furnished home office.

The bad

It’s still a cheap chair that will wear out in a couple years. The mechanism underneath the seat looks almost identical to the ones on all of my previous cheap chairs and nothing like the ones on the professional chairs we use at work.

The tall wheels have a couple side effects. First, the legs that attach the wheels are now a little higher than normal. Achilles tendon height. If you roll it into your heal while sitting it will break skin and hurt. Trust me. Secondly, the taller wheels mean the lowest setting for the seat height in taller than my previous chairs.