Monday, October 6, 2008

No More NDA! More Posts Coming Very Soon... + Book Recommendation

With the NDA having been lifted and training materials becoming available I am getting back into iPhone Development. I am currently reading 'iPhone SDK Development' by The Pragmatic Bookshelf™, and I would recommend it. You can find it here: http://pragprog.com/titles/amiphd/iphone-sdk-development (you can only buy the eBook for now).

You can expect more posts pretty soon, along with some additional getting started tips now that I understand the SDK a little better.

Sunday, June 22, 2008

Programming in Objective-C with Xcode

I'll start with an excerpt from my last post:
"I finally settled on a book by Stephen G. Kochan titled Programming in Objective-C. I am pretty well into the book now and I would definitely recommend it if you're new to Objective-C and/or OOP. Click here to check out the book on Amazon."

If this isn't the book you're using then this post won't be too relevant to you, as I'll be outlining some things specific to it in the post. You should still read it though, especially if you haven't yet decided on what resource you're going to choose for learning Objective-C.

The second half of the post is geared towards writing Objective-C in Xcode for the programs and exercises in the book. You're going to live in Xcode while developing iPhone applications, so you may as well get familiar now. There's a brief mention of Xcode in the book, but it wasn't ready yet when the book was released so I'll give you a little guidance to get started.

The Book: Programming in Objective-C

So far so good. It's living up to it's product description very well.
"Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming. The book makes no assumption about prior experience with object-oriented programming languages..."

Click here for a very detailed review, the one that sold me on the book. In that review are two very important suggestions that I would heed to. The first is the errata, and I will get to the second in the next half of this post about Xcode.
  • If you have the first run of this book (which has a Lighthouse on the front) you'll definitely want to print out the errata found here (PDF) and keep it handy, or make notes right in the book. The second run of the book (which has a merry-go-round on the front) has been updated.
Here are a few general notes on Objective-C from the first two chapters of the book:
  • Objective-C files should always end with a .m extension.
  • Objective-C is case-sensitive.
  • Similar to PHP (amd many other languages probably) you can single line comment with // and multi-line comment with /* */.
  • All program variables must be defined before they can be used.

Using Xcode to Write Objective-C

As you get through the first two chapters of the book you will begin writing a few simple programs. These tips should help get you on your way to writing those programs in Xcode:
  • To start a new project in Xcode: File Menu -> New Project -> Select Application under Mac OS X
    -> Cocoa Application
  • Each time you begin a new project, before writing any code you'll want to delete the entire contents of the <project_name>_Prefix.pch file and save it. Also delete the #import <cocoa/cocoa.h> line at the top of main.m. If you don't, your programs won't compile properly. Thanks to Kayembi on Amazon for pointing these out.
  • This is a matter of personal preference, but I like having line numbers in my code editor. To turn them on in Xcode go to Preferences Menu -> Text Editing -> Select Show Line Numbers
  • Lastly, I had a hard time finding the output of my first few programs. I would select "Build and Go" in XCode but never see anything aside from the message that says "Debugging of <project_name> ended normally." Then after a little poking around I discovered in Xcode if you go to Window Menu -> Organizer, then at the bottom select the small plus sign and choose the "Add Existing Folder" option, then choose the directory where you stored your first programs. Once your projects show up in Organizer you can select the folder for that program and click "Run". Provided you have already run "Build and Go" in Xcode, that window will show you the output of the program. Many of the programs and exercises in the book output simple strings, and this is a great way to see that output. In the same output window you can click "Clear log" on the right to clear the output if you want.
    UPDATE, AN EASIER WAY: "MaximumTheLyricist" commented the following: "I noticed that there is an easier way. If you go to the Run Menu in xcode then Console, you will see the output and save computer screen real estate." Thanks MTL!
At this point you're pretty much caught up with me. I've gotten through the first two chapters, and I can write a couple basic programs in Objective-C. I am about to crack Chapter 3: Classes, Objects, and Methods which is where the real (OOP) challenge begins!

The iPhone SDK is Installed. Now What?

Once my system was all setup, and the iPhone SDK installation completed, the next logical step was to make the tools I needed to develop iPhone applications available from the dock. I added aliases for Xcode, Instruments, and Interface Builder from the Developer directory (or wherever you chose to install the SDK). If you're new to Mac, you can do this by dragging the icon for an application down to the dock in the position you would like for it to be.


Getting Started Videos

At this point, if you're anything like me, you're eager to crack open any one of the development tools and do something, anything. Well, not so fast. That didn't get me anywhere. First I took the high-road and spent a week reviewing the "Getting Started Videos" in the iPhone Dev Center. I would definitely recommend you do the same, as the videos are an absolutely necessary introduction to developing iPhone applications.

"Watch Apple experts discuss everything from getting started with the iPhone SDK, to using the development tools and technologies necessary to create applications for iPhone."
You will need to be logged in to view the content. It took me about 10 hours to watch and take thorough notes on all 11 videos. A very small portion of the material is redundant, but it's still worth watching all of them, in their entirety.


Getting Started Documents / Learning Objective-C

I got through the videos and I was pretty motivated to keep rocking, but not comfortable enough to open Xcode and take a crack at anything of my own just yet. The logical progression for me led to the next section of the iPhone Dev Center, which is "Getting Started Documents".

"Read about the tools, frameworks, development best-practices, and design methods for creating your iPhone application."
The first two documents (which are really just web pages) didn't introduce anything new, and I was a little disappointed. Then the third document 'Learning Objective-C: A Primer' finally shined some guiding light on the next step in the learning process. The third paragraph opens by saying:

"If you have never programmed using an object-oriented language before, you need to have at least a basic understanding of the associated concepts before proceeding."
As you learned in the introductory videos, Objective-C is the language you need to know to write Cocoa and Cocoa Touch applications, and I'm going to take a wild-guess that if you're reading this post you don't know Objective-C. Well, I don't either. I don't know any application programming language for that matter. I only know the basic principles regarding Object Oriented Programming. I needed a book on Objective-C; a damned good book.

I poured over Amazon for a few minutes, and after reading the customer reviews I finally settled on a book by Stephen G. Kochan titled Programming in Objective-C. I am pretty well into the book now (to be covered in my next post) and I would definitely recommend it if you're new to Objective-C and/or OOP. Click here to check out the book on Amazon.

Hopefully by now you have an idea of the basic work flow and iPhone SDK tools associated with developing iPhone applications. In my next post I will be covering my experiences with the book mentioned above, some general information on Objective-C, and finally a few useful Xcode tips that helped me along through my first few exercises in the book.

Setting Up Your Mac to Develop iPhone Applications

The bare minimum needed to develop native iPhone applications is a Mac with OS X Leopard, and the iPhone SDK. That's it. I will be developing iPhone apps on a Mac Mini.

  • 1.83GHz Intel Core 2 Duo
  • 1GB memory
  • OS X Leopard

PC / Mac "Hybrid"? Oh yes!

I still prefer a PC for day-to-day (we'll see how long that lasts) stuff, but I did want to have the Mac very accessible at my workspace without the clutter of two mice and two keyboards on the same desk, so I setup Synergy. At this point I have a Mac on my left monitor and a PC on my right monitor.

Synergy is a great Open Source software that will allow you to share a single mouse and keyboard (and clip board) between your Mac and PC (or *nix box, if you prefer). You will need to have a dual monitor (or more) setup for this solution to make sense. It's fairly easy to setup, and pretty effortless once you're up and running.

Here are a few Synergy tips based on my experience with it so far:
  1. Use your system IP addresses in place of host names during setup, I had trouble with host names.
  2. Make sure Synergy is in startup on both machines, it's a hassle otherwise.
  3. If you need to restart the machine (PC in my case) that acts as the Synergy server you have to shut both systems down, boot the server, and then boot the client (Mac in my case).
  4. If you're sharing a PC keyboard (I haven't tried sharing the Mac keyboard) the ALT Key is equal to the Command Key on a Mac. It's been a week and I'm still not quite used to this.
  5. If you have a spare USB mouse lying around I would connect it to the Mac and tuck it away somewhere hidden, you'll want it to diagnose any issues. I have heard reports of screen savers sticking as well, but haven't personally experienced any issue.
Download Synergy


Installing the iPhone SDK

Once your Mac is setup the next step is to install the iPhone SDK. You can download the iPhone SDK by visiting the Apple iPhone Dev Center. You will need to register for a free iPhone Developer account to download the SDK. The download is over 1GB in size, so be prepared to hang out for a few minutes while that finishes.

Once you have the SDK downloaded, installing is a snap. Actually, to be honest, it hung twice on me the first two-times I tried installing it. However, a couple of force-quits and a restart later, it installed fine. I installed by selecting all of the available packages, but left the install path and any other settings to their default values/locations. If you would like to get "crafty" with your install there is an 'iPhone SDK Readme' PDF located under the Downloads section of the iPhone Dev Center.

The SDK includes all of the essential applications you need to develop iPhone Applications. Apple calls it "iPhone SDK and Xcode 3.1 Developer Tools for Mac OS X v10.5.3". Xcode is the IDE (Integrated Development Environment) that you will be getting cozy with throughout the development cycle.

My next post will pick up from this point, where your system is all setup and you have the iPhone SDK installed.