Category Archives: iPhone

iOS NSStreamDelegates, Grand Central Dispatch, & NSNotificationCenter

One of the more difficult design patterns I’ve managed to get working in Objective-C while developing an iOS app is one that requires NSStreamDelegates, Grand Central Dispatch (GCD) and NSNotificationCenter.  This design pattern comes into play in the following situation:

  • Application launches
  • Application does some communication with a server in the background
  • When the server communication in the background is complete, the View Controller is updated to display information from the server.

Its a pretty common scenario and I’m sure one that has many answers.  In my case, I needed to use CFReadStreams and CFWriteStreams due to the way the client server communication works.  So if there was a simpler way to do it, I’d love to know.

The design pattern that I used at a high level to accomplish this is as follows:


  • Application launches,  displays its first View Controller
  • This first View Controller in viewDidLoad registers an observer with NSNotificationCenter and then kicks off a background request via GCD to do all the client/server communication
  • The background request that launches is an NSStreamDelegate object.  It sends info to the server.
  • Once the background request receives information from the server, it sends a notification to NSNotificationCenter that the client/server communication is complete
  • Finally, the first View Controller gets this notification, and then updates its View with that info.

That’s the high level.  So now lets go through and flesh out the details.

Application Launch and First View Controller

This is a simple example, but its just to get you the basic idea.  This part is standard in most iOS apps.  The thing to note about this is that my FirstViewController is not an NSStreamDelegate.  It does however contain an object that is an NSStreamDelegate.  This is called my Communicator class.

First View Controller viewDidLoad

Here’s where we start to get cool.  viewDidLoad looks something like this:

A few things to note here:

1.  We add an observerer to NSNotificationCenter.  This basically says:  If someone sends the updateView message, then I’ll respond to it with my updateView method.  (We’ll show this later)

2.  We dispatch an asynchronous task to GCD.  That’s what this whole dispatch_async business is all about.  This way, the view controller will just display while this task runs in the background.

3.  We instantiate myComm and initiate it with some connection info.  This info might be something you got from the app delegate, some other view controller, or a plist.  That part is left to you to decide.

NSStreamDelegate actions

Our class Communicator is a NSStreamDelegate.  Here’s some of the relevant header info:

Nothing big here, just the NSInputStream (the stream we get info from the server) and the NSOutputStream (The stream we write to the server)

As part of the initialization of the Communicator, we have a function called startConnection. This can be called in the init methods. (provided you have all the server communication info: (e.g: user, password, server, port, etc).

Our’s has this:

The important part here is what you do before you schedule your input and output streams on the RunLoop.  The RunLoop is the preferred way to work with NSStreamDelegates.  You have to make sure that you put it running back on the main thread’s runloop.  That’s what this whole dispatch_async(dispatch_get_main_queue() bit is about.  If you leave this out, then you’ll never get any connections going on.

Once you open the connections and you do your reading and writing in the handleEvent function:

Then once you finally get the data from the input stream you send a notification.  This is done for us in the

we run:

Before we do that, however, we set our theResult NSString to the value we received.

Updating View in First View Controller

Back in the FirstViewController, we have a function updateView:

Notice here that once again we need to make sure it goes back on the main thread before we update the view. Then, since our Communicator belongs to our ViewController, we can get the result of the client/server communication through the getter and set the UILabel to that result.


As mentioned before, there are probably lots of ways to do this and some are probably better than what I’ve mentioned here.  However, this was pretty tricky to me to figure out and once I did, I thought it was worth sharing as I couldn’t find any holistic info on it via any search engines.

I’d be curious to know if this helps anybody.  Thanks for reading.

UCS Tech Specs

UCS Tech Specs

My first iOS app has been published on iTunes! It’s called UCS Tech Specs. The purpose of the app was really just to help me get more familiar with the Cisco UCS product line. I have a hard time searching for tech sheets on my computer that have all the details people ask me about a specific server. Having it in an iPhone app is pretty handy! I hope it helps other people as well.

What does it do?
It has technical specifications for the most common UCS products. The fabric interconnects, IO modules, different blades, etc. If someone were to ask you: “How many DIMM slots are available on a C250 M2?” then you could quickly look it up. It’s basically just a series of tables that allow you to drill down to different components of a UCS product.

Who will support it going forward?
Me. I plan on spending an hour or two each month updating with the latest parts. The more people that use it, the more time I’ll spend supporting it. The way I’ve set up the app makes it really easy to add or remove components.

What are future enhancements we’ll see in this app?
There are two main features I’d like to add:
1. I’d like to make the iPad app a little different. I’d like to add bigger pictures, and more interactive.
2. I’d like to make the app check for updates if there is a internet connection. That way, I wouldn’t have to update it in Xcode for changes. It would pull updates from an Internet site. This would make updates faster and make it easier for other people to add changes to the app without having to muck around with the code. I’m working on that right now.

I have a suggestion/comment/correction/complaint for you, how should I get it to you?
I’d love any feedback you have. Just email/twitter/comments here. If you just want to talk about iOS and why my app sucks, that’s ok too.

iOS app submitted to iTunes Connect

I just submitted my first app to iTunes using Xcode4 and iTunes Connect. There was a bit of a learning curve, but after hacking away at it last night I’m happy to say its sitting in Apple’s queue waiting for review.

First off, let me say that even though its a total pain there are some good things that come of it: Because of the validation process they make you go through you don’t have to wonder if there is technically something wrong with your app. The validation phase takes care of making sure you have the basic technical aspects of your app working correctly. This includes verifying certificates are correct, making sure nothing offensive is sent to the store, and making sure the code actually compiles and runs.

Perhaps the most difficult thing for me was to sit down and actually read the manual. The part I missed during the entire development process was the iOS portal:

iOS developer portal

This is where you look to upload to the portal

This is found when you log into the iOS developer center.

The first thing I missed was that I needed to create a ‘Distribution Certificate’.

This distribution certificate then needed to be downloaded into my Keychain. Basically, once this is done, you click on the file to download it, then click on the downloaded file and it will be inside your keychain. Not too hard right?

The next step is to actually create an App ID. I found this part frustrating as you are not allowed to delete an app id once it is created. In addition to this the App ID you create inside of iOS portal must be the same as the bundle ID of the app as well as the bundle ID that you give to iTunes connect. (More on iTunes connect in a bit). This is the part that took me forever because I renamed my app several times. I started with UCS Tech Ref, then UCS Tech Specs. So my bundle ID was ucstechref, then I changed to ucstechspecs. The problem is that in iTunes Connect, I had already named it ucstechref and I wasn’t allowed to change it.

Anyway, once the app ID was created, I configured them for push downloads by doing the process it describes in the configuration section of App ID. When finished my App ID had these things configured:

The last step in the portal was to create a “Distribution Provisioning Profile” for the App itself.

And that was it for the iOS portal. Now keep in mind that before I ran through the iOS portal to create my App IDs and Distribution provisioning profile, I had to actually create the app in iTunes Connect. This was pretty self explanatory.

The last step was putting it all through in Xcode. First, you need to open the Distribution Provisioning Profile and drag it into the Xcode Organizer. (Open Xcode and click shift+command+2). Once done here, you need to code sign your app:

The final part is to make sure your -info.plist has the Bundle Identifier set to the App ID that you specified in iTunes Connect and the App ID in the iOS Portal.


Now you have to make sure that you have lots of pictures and the right sizes for all of them! That part was actually kind of enjoyable for me. I use Pixelmator to do all my art work and its pretty easy to get the hang of.

Once you have the right icons specified, go into your product Build Settings and check the ‘Validate Built Product’ and you’ll be warned of any issues you have.

Once that passes, you Archive the application, and can now validate, share, or distribute.

Words can not express the relief I felt after I finally validated the app and it passed all the inspections. The next step was to submit the app. I was stoked. App submitted. But then, I got an email from Apple immediately stating that my app was missing an image! I had forgotten an iPad screenshot. I quickly took one, submitted it in iTunes Connect and I am now back to hurry up and wait mode.

All in all a pretty rigorous process. However, I think its good because it filters a lot of junk from getting on to iTunes. Even though some apps are pretty weak in the iTunes store, they still pass enough hoops to make sure that they’re not complete garbage. I’m hoping my users will at least think that about my app… but if they don’t, hey, what do you expect for free?

Welcome to iPhone development

Last year I learned Ruby and Ruby on Rails.  So in keeping with that good advice to learn a new program language every year, I thought this year I would tackle objective C and Apple’s IDE (xcode).  Why not?  iPhones are fun and the only reason I was able to talk my wife into letting me fork out the money for a Mac was because I told her I wanted to write iPhone apps.  Well, 3 years later and I’m finally doing it…

So here’s how I’m starting:


As I’ve been researching the best book to buy or examples to find, I’m always struck by how readily information is available to any seeker of wisdom.


There are lots of books about iPhone development and I read the first chapter of several of them.  I ended up accidently clicking the Beginning iPhone 4 Development: Exploring the iOS SDK from Amazon which I suppose wasn’t bad since it seems to be the top recommended book.  The book will probably arrive sometime this week.  But as I checked out the first chapters of the one by the Big Nerd Ranch, I really liked how they presented and explained things.  So that’s it for books.  O’Reilly has a deal today as well for $13 for a book called Cocoa & Objective-C: Up&Running .  Its $14 with the 50% discount code of DDCRN. There seems no shortage of people who want to tell you how to code for the iPhone as long as you are willing to pay for it.

Apple Resources

I was able to download from Apple’s iBook store the free iOS Application Programming Guide, which was actually pretty well written and I’m sure I’ll go back to.  It is about 333 pages and tells how apps run on an i* device.  Many of the concepts I’m sure I’ll forget but hopefully when I run into a serious bug I’ll come back to it and say doh!  its cause of _______.  I actually really like this doc (for now) as it provides a theory which is something a person who already knows how to program needs to get familiar with to get up and running on this platform.  This is a huge recommendation.  There are also several other Apple docs referenced in this pdf file that shouldn’t be overlooked.  Included in this is “The Objective-C  Programming Language”


Here was where the real treasure chest was found.  The course given at Stanford university is offered on YouTube and is also available from iTunes complete with power point documentation.  Watching the last half of the first class and I already created my first app with a slider that changes a label.  The Stanford class doesn’t use any books and instead references Apple documentation.  It seems to be a great way to start learning. I figure if I do one lecture a day (or 1 every two days where I follow along ) then I’ll be in very good shape.

Other Online Resources

In addition to iTunes U and YouTube there are countless other sites where people freely share information.  Just google the class you want to know about and you’re there.  But how can you beat getting taught from Stanford professors who are also employees of Apple?

The first program

So after using xcode and generating the slider demo from the Stanford class I thought I’d first just try to compile using good old gcc and a basic program.  There was a good wiki article that explained the history of the language.  One of the important things I read was: “In Objective-C one does not call a method; one sends a message” so as a future reference, I have to remember that objects are sent messages to run what would be a method in most normal OOP languages.  The  other few important things:

  • Classes are declared in header files and methods are implemented in the *.m files (which used to stand for messages)
  • In the header file a (-) in front of a method means its an instance method.  A (+) in front of the method means its a class method (similar to how in Ruby we do a

So here’s a program named  nothing.m that does nothing:

Then, I just compile it on the command line:

Running nothing:

Not very interesting, but hey, its a program and it did what you told it to do.  You can then get fancy and add a printf or something else.  But from here on out, I’ll probably be in xcode as that seems to be what all the cool kids are doing.

What are some important beginner tips you have?

iPhone App development

Last night I tried my hand at iPhone development.  I watched the Stanford classes on iTunes U.  The first class by Evan Doll was ok.  Since I’ve been programming for a while, really the only thing that helped was the last 15 minutes where he gave a demo of it.  The demo didn’t work and there were also some differences in Xcode with my setup (either that or its the new 3.0 stuff.)  I was able to figure it out and get it working.

2 Gotchas:

1.  Make sure you save the .xib file in Interface builder before compiling and running in Xcode with apple-R

2.  The part I was stuck on was adding the Actions and Outlets.   I couldn’t do this in the Inspector window like he did.  I created the class in the Inspector, but then had to add the Action and Outlets in the Tools->Library.  Select the class in the list of all the classes and then you can edit it below.

Once that was done, I could follow along with the rest.  It was a good way to jump into iPhone development.  For me, since I come from the world of no IDE’s and just VI, this was a good intro.  Hopefully I’ll be able to get more into it in the future.  It certainly looks like a cool platform to develop on.

The State of My iPhone

The State of my iPhone is good.  I’m super happy with it, and its by far the best phone I’ve ever had.  I can’t say I’ve ever had a blackberry, but from what I’ve seen from the competition I have not been impressed.  So I remain an Apple fan, and I don’t think the Android phone is ever going to beat it.  The iPhone is to smart phones as Disneyland is to amusement parks.  There are some that have compelling offerings, like open source, an open market, or a faster/bigger roller coaster.  But having the business model where the entire experience is completely controlled is what makes Apple work, and probably the reason they are cringing at AT&T every day.

Since I’ve gone to the iPhone 3.0.1 I’ve stopped jail breaking my phone.  The only reason I had jailbroken the phone in the past was because I wanted tethering, video, and Mike Tyson’s Punch-Out.

The video now comes with the iPhone 3GS, but on my iPhone 3G, I don’t have such luxury.  But it turns out I didn’t use it all that much.  So no big loss.  Also the footage I came out with wasn’t all that great.  I guess I don’t have that exciting of a life.  So no big deal losing that.


Tethering is the act of using your iPhone as a modem for your PC to get you on line.  I used to use PDA net from the jail break stuff.  But my friend recently showed me the link below:

From here it was easy to get tethering.  I opened the above URL from my iPhone, followed the prompts, and now my iPhone can get my computer on line and I can do work.

As far as Mike Tyson’s Punch out, I think I’ll be content after having made it to Bald Bull 2.  So I no longer have an NES emulator on my iPhone.

Application Updates

My top apps:

  1. Pandora
  2. Facebook
  3. TweedtDeck
  4. Classics
  5. The Weather Channel
  6. iSSH
  7. Shakespeare
  8. Constitution
  9. AP Mobile
  10. Bump
  11. vlingo
  12. GPSLite

The last 3 are ones that my friend just introduced me to.