Chapter 1. Welcome to the Jungle

So, you want to write iPhone applications? Well, we can't say that we blame you. iPhone might just be the most interesting new platform to come around in a long time; certainly, it is the most interesting mobile platform to date, especially now that Apple has provided a set of elegant, well-documented tools for developing iPhone applications. And with the recent release of version 3.0 of the iPhone software development kit (SDK), things have only gotten better.

Note

This book has been newly revised and updated to work with the latest version of the SDK. In some places, we have chosen to use new functions or methods introduced with version 3.0 that may prove incompatible with earlier versions of the SDK. We'll be sure to point those situations out as they arise in this book.

Be sure to download the latest and greatest source code archives from the book's web site at http://iphonedevbook.com.

We've added conditional macros to that code to allow it to build with the latest version of the SDK, as well as with older versions of the SDK. We'll update the code as new versions of the SDK are released, so be sure to check the site periodically.

What This Book Is

This book is a guide to help you get started down the path to creating your own iPhone applications. Our goal is to get you past the initial learning curve to help you understand the way iPhone applications work and how they are built. As you work your way through this book, you will create a number of small applications, each designed to highlight specific iPhone features and show you how to control or interact with those features. If you combine the foundation you'll gain by making your way through this book with your own creativity and determination and then add in the extensive and well-written documentation provided by Apple, you'll have everything you'll need to build your own professional iPhone applications.

Note

Dave and Jeff have a forum set up for this book. It's a great place to meet like-minded folks, get your questions answered, and even answer other people's questions. It's at http://iphonedevbook.com/forum. Be sure to check it out!

What You Need Before You Can Begin

Before you can begin writing software for iPhone, you'll need a few things. For starters, you'll need an Intel-based Macintosh running Leopard (OS X 10.5.6 or later). Any Macintosh computer—laptop or desktop—released since mid-2006 should work just fine.

You do not need a top-of-the-line model to get started, so a MacBook or Mac Mini will serve admirably. The older and slower the model, the more it will benefit from a RAM upgrade, however.

You'll also need to sign up to become a registered iPhone developer. Apple requires this step before you're allowed to download the iPhone SDK.

To sign up, navigate to http://developer.apple.com/iphone/, which will bring you to a page similar to the one shown in Figure 1-1. Somewhere on the page is a link to the latest and greatest iPhone SDK. Click the link, and you'll be brought to a sign-up page with three options.

Apple's iPhone Dev Center web site

Figure 1.1. Apple's iPhone Dev Center web site

The simplest (and free) option is to click the button that reads Download the Free SDK. You'll be prompted for your Apple ID. Use your Apple ID to log in. If you don't have an Apple ID, click the Create Apple ID button, create one, and then log in. Once you are logged in, you'll be taken to the main iPhone development page. Not only will you find a link to the SDK download, but you'll also find links to a wealth of documentation, videos, sample code, and the like, all dedicated to teaching you the finer points of iPhone application development.

One of the most important elements included with the iPhone SDK is Xcode, Apple's integrated development environment (IDE). Xcode includes tools for creating and debugging source code, compiling applications, and performance tuning the applications you've written. By the time you are finished with this book, you will become an Xcode aficionado!

The free SDK also includes a simulator that will allow you to run most iPhone programs on your Mac. This is perfect for learning how to program your iPhone. The free option will not, however, allow you to download your applications onto your actual iPhone (or iPod touch). It also does not give you the ability to distribute your applications on Apple's iPhone App Store. For that, you'll need one of the other two options, which aren't free.

Note

The simulator does not support hardware-dependent features, such as iPhone's accelerometer or camera. For those, you'll need the alternate options as well. Just thought you'd like to know!

The Standard program costs $99. It provides a host of development tools and resources, technical support, distribution of your application via Apple's App Store, and, most importantly, the ability to test and debug your code on an iPhone rather than just in the simulator.

The Enterprise program costs $299 and is designed for companies developing proprietary, in-house applications for iPhone and iPod touch and for those developing applications for the Apple's App Store and with more than one developer working on the project.

For more details on these two programs, visit http://developer.apple.com/iphone/program/.

Because iPhone is an always-connected mobile device that uses other companies' wireless infrastructure, Apple has had to place far more restrictions on iPhone developers than it ever has on Mac developers, who are able to write and distribute programs with absolutely no oversight or approval from Apple.

Apple has not added restrictions to be mean but rather is trying to minimize the chances of malicious or poorly written programs being distributed that could degrade performance on the shared network. Developing for the iPhone may seem like it presents a lot of hoops to jump through, but Apple has gone through quite an effort to make the process as painless as possible. It should be noted too that $99 is still considerably less than buying, for example, Visual Studio, which is Microsoft's software development IDE.

This may seem obvious, but you'll also need an iPhone or iPod touch. While much of your code can be tested using the iPhone Simulator, not all programs can be, and even those that can really need to be thoroughly tested on an actual iPhone before you ever consider releasing your application to the public.

Note

If you are going to sign up for the Standard or Enterprise program, you should go do it right now. The approval process can take a while, and you'll need that approval to be able to run your applications on your iPhone or iPod touch. Don't worry, though, because all the projects in the first several chapters and the majority of the applications in this book will run just fine on the iPhone Simulator.

What You Need to Know Before You Begin

This book assumes that you already have some programming knowledge. It assumes that you understand the fundamentals of object-oriented programming such as what objects, loops, and variables are, for example. It also assumes you are familiar with the Objective-C programming language. Cocoa Touch, the part of the SDK that you will be using through most of this book, uses Objective-C 2.0, but don't worry if you're not familiar with the more recent additions to the Objective-C language. We'll be sure to highlight any of the 2.0 language features we take advantage of and explain how they work and why we are using them.

You should also be familiar with the iPhone itself. Just as you would with any platform for which you wanted to write an application, get to know the iPhone's nuances and quirks, and get familiar with the iPhone interface and with the way Apple's iPhone programs look and feel.

What's Different About Coding for iPhone?

If you have never used Cocoa or its predecessor NextSTEP, you may find Cocoa Touch, the application framework you'll be using to write iPhone applications, a little alien; there are some fundamental differences from other common application frameworks such as those used when building .NET or Java applications. Don't worry too much if you feel a little lost at first. Just keep plugging away at the exercises, and it'll all start to fall into place after a while.

If you have written programs using Cocoa or NextSTEP, you're going to find a lot in the iPhone SDK that is familiar to you. A great many classes are unchanged from the versions that are used to develop for Mac OS X, and even those that are different tend to follow the same basic principles and use design patterns similar to the ones you are already familiar with. There are, however, several differences between Cocoa and Cocoa Touch.

Regardless of your background, you need to keep in mind some key differences between iPhone development and desktop application development.

Only One Running Application

With the exception of the operating system itself, only one application can be running at any given time on an iPhone. This may change in the future as iPhone gets more memory and more powerful processors, but for the time being, your application will be the only one running while your code is executing. When your application isn't the one the user is interacting with, it won't be able to do anything.

Only One Window

Unlike desktop and laptop operating systems where many running programs coexist, each with the ability to create and control multiple windows, iPhone gives your application just one "window" to work with. All of your application's interaction with the user takes place inside this one window, and its size is fixed at the size of the iPhone screen.

Limited Access

Unlike programs on a computer that pretty much have access to everything the user who launched them does, iPhone seriously restricts what your application can get to. You can read and write files only from the part of iPhone's file system that was created for your application. This area is called your application's sandbox, and it is where your application will store documents, preferences, and every other kind of data it may need to store.

Your application is also constrained in some other ways; you will not be able to access low-number network ports on iPhone, for example, or do anything else that would typically require root or administrative access on a desktop computer.

Limited Response Time

Because of the way it is used, iPhone needs to be snappy and expects the same of your application. When your program is launched, you have to get your application open, preferences and data loaded, and the main view shown on the screen as fast as possible—in not more than a few seconds. At any time when your program is running, it may have the rug pulled out from under it. If the user presses the home button, iPhone goes home, and you have to quickly save everything and quit. If you take longer than five seconds to save and give up control, your application process will be killed, regardless of whether you are finished saving.

As a result, you have to carefully craft your iPhone applications to make sure data is not lost when the user quits.

Limited Screen Size

iPhone's screen is really nice. When introduced, it was the highest-resolution screen available on a consumer device, by far. But the iPhone display just isn't all that big, and as a result, you have a lot less room to work with than on modern computers, just 320 × 480 pixels. To give an interesting contrast, at the time of this writing, Apple's least expensive iMac supports 1680 × 1050 pixels, and its least expensive notebook computer, the MacBook, supports 1280 × 800 pixels. On the other end of the spectrum, Apple's largest monitor, the 30-inch Cinema Display, offers a whopping 2560 × 1600 pixels.

Limited System Resources

Any old-time programmers who are reading this are likely laughing at the idea of a machine with at least 128MB of RAM and 4GB of storage being in any way resource constrained, but it is true. Developing for the iPhone is not, perhaps, in exactly the same league as trying to write a complex spreadsheet application on a machine with 48KB of memory, but given the graphical nature of iPhone and all the things it is capable of doing, running out of memory is very, very easy. The versions of iPhone available right now all have either 128MB or 256MB of physical RAM, though that will likely increase over time. Some of that memory is used for the screen buffer and by other system processes. Usually, no more than half of that memory is left for your application to use, and it can be considerably less.

Although that may sound like it leaves a pretty decent amount of memory for such a small computer, there is another factor to consider when it comes to memory on iPhone: modern computer operating systems like Mac OS X will take chunks of memory that aren't being used and write them out to disk in something called a swap file, which allows applications to keep running even when they have requested more memory than is actually available on the computer. The iPhone OS, however, will not write volatile memory, such as application data, out to a swap file. As a result, the amount of memory available to your application is constrained by the amount of unused physical memory in the phone.

Cocoa Touch has built-in mechanisms for letting your application know that memory is getting low. When that happens, your application must free up unneeded memory or risk being forced to quit.

No Garbage Collection

We mentioned earlier that Cocoa Touch uses Objective-C 2.0, but one of the key new features of that language is not available on iPhone: Cocoa Touch does not support garbage collection.

Some New Stuff

Since we've mentioned that Cocoa Touch is missing some features that Cocoa has, it seems only fair to mention that the iPhone SDK contains some new functionality that is not currently present in Cocoa or, at least, is not available on every Mac. The iPhone SDK provides a way for your application to determine the phone's current geographic coordinates using Core Location. iPhone also has a built-in camera and photo library, and the SDK provides mechanisms that allow your application to access both. iPhone also has a built-in accelerometer that lets you detect how your iPhone is being held and moved.

A Different Approach

Two things iPhone doesn't have are a physical keyboard and a mouse, which means you have a fundamentally different way of interacting with the user than you do when programming for a general-purpose computer. Fortunately, most of that interaction is handled for you. If you add a text field to your application, iPhone knows to bring up a keyboard when the user clicks in that field, for example, without you having to write any extra code.

What's in This Book

Here is a very brief overview of the remaining chapters in this book.

In this chapter, we'll learn how to use Xcode's partner in crime, Interface Builder, to create a simple interface, placing some text on the iPhone screen.

In Chapter 3, we'll start interacting with the user, building a simple application that dynamically updates displayed text at runtime based on buttons the user presses.

Chapter 4 will build on Chapter 3 by introducing you to several more of iPhone's standard user interface controls. We'll also look at how to use alerts and sheets to prompt users to make a decision or to inform them that something out of the ordinary has occurred.

In Chapter 5, we'll look at handling autorotation, the mechanism that allows iPhone applications to be used in both portrait and landscape modes.

We'll move into more advanced user interfaces in Chapter 6 and look at creating multiview interfaces. We'll change which view is being shown to the user at runtime, allowing you to create more complex user interfaces.

Toolbar controllers are one of the standard iPhone user interfaces; in Chapter 7, we'll look at how to implement this kind of interface.

In Chapter 8, we'll look at table views, the primary way of providing lists of data to the user and the foundation of hierarchical navigation-based applications and also see how to let the user search in your application data.

One of the most common iPhone application interfaces is the hierarchical list that lets you drill down to see more data or more details. In Chapter 9, you'll see what's involved in implementing this standard type of interface.

In Chapter 10, we'll look at implementing application settings, which is iPhone's mechanism for letting users set their application-level preferences.

Chapter 11 looks at data management on iPhone. We'll talk about creating objects to hold application data and see how that data can be persisted to iPhone's file system. We'll also see the basics of using something called Core Data, which allows you to save and retrieve data easily.

Everybody loves to draw, so we'll look at doing some custom drawing in Chapter 12, using basic drawing functions in Quartz and OpenGL ES.

iPhone's multitouch screen can accept a wide variety of gestural inputs from the user. In Chapter 13, you'll learn all about detecting basic gestures such as the pinch and swipe. We'll also look at the process of defining new gestures and talk about when new gestures are appropriate.

iPhone is capable of determining its latitude and longitude thanks to Core Location. We'll build some code that makes use of Core Location to figure out where in the world your iPhone is and use that information in our quest for world dominance.

In Chapter 15, we'll look at interfacing with iPhone's accelerometer, which is how your iPhone knows which way it's being held. We'll look at some of the fun things your application can do with that information.

Each iPhone has a camera and a library of pictures, both of which are available to your application, if you ask nicely! In Chapter 16, we'll show you how to ask nicely.

iPhone is currently available in 80 countries. In Chapter 17, we'll show you how to write your applications in such a way that all parts of your application can be easily translated into other languages to expand the potential audience for your applications.

At this point in the book, you'll have mastered the fundamental building blocks for creating iPhone applications. But where do you go from here? In Chapter 18, we'll explore the logical next steps for you to take on your journey to master the iPhone SDK.

What's New in This Update?

Since the first edition of this book hit the bookstores, lots has been happening in the iPhone development universe. The growth of the iPhone development community has been phenomenal. The SDK has continually evolved, with Apple releasing a steady stream of updates to SDK 2. In March 2009, Apple announced a major release, SDK 3.0. Apple's iPhone team sure has been busy.

Well, we've been busy, too! The second we found out about SDK 3.0, we immediately went to work, updating every single project to ensure not only that the code for each one compiles under the new version of the SDK but also that each one takes advantage of the latest and greatest features offered by Cocoa Touch. We reshot a boatload of screenshots; tweaked the prose throughout the book; and, in Chapter 11, added a brief introduction to Core Data, one of the most exciting new parts of iPhone SDK 3.

Are You Ready?

iPhone is an incredible computing platform and an exciting new frontier for your development pleasure. Programming your iPhone is going to be a new experience, different from that of any platform you've worked with before. For everything that looks familiar, there will be something alien, but as you work through the book's code, the concepts should all come together and start to make sense.

You should keep in mind that the exercises in this book are not simply a checklist that, when completed, magically grants you iPhone developer guru status. Make sure you understand what you did, and why, before moving on to the next project. Don't be afraid to make changes to the code; experimenting and observing the results is one of the best ways you can wrap your head around the complexities of coding in an environment like Cocoa Touch.

That said, if you've got your iPhone SDK installed, turn the page. If not, get to it! Got it? Good. Then let's go!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.12.230