Introduction

An aspiring iPhone developer faces three basic hurdles:

You must learn the Objective-C language. Objective-C is a small and simple extension to the C language. After the first four chapters of this book, you will have a working knowledge of Objective-C.

You must master the big ideas. These include things like memory management techniques, delegation, archiving, and the proper use of view controllers. The big ideas take a few days to understand. When you reach the halfway point of this book, you will understand these big ideas.

You must master the frameworks. The eventual goal is to know how to use every method of every class in every framework on the iPhone. This is a project for a lifetime: there are over 3000 methods and more than 200 classes available for the iPhone. To make things even worse, Apple adds new classes and new methods with every release of the iPhone OS. In this book, you will be introduced to each of the subsystems that make up the iPhone SDK, but we will not study each one deeply. Instead, our goal is get you to the point where you can search and understand Apple’s reference documentation.

We have used this material many times at our iPhone Development Bootcamp at Big Nerd Ranch. It is well-tested and has helped hundreds of people become iPhone application developers. We sincerely hope that it proves useful to you.

Prerequisites

This book assumes that you are already motivated to learn to write iPhone apps. We won’t spend any time convincing you that the iPhone is a compelling piece of technology.

We also assume that you know the C programming language and something about object-oriented programming. If this is not true, you should probably start with an introductory book on C and Objective-C. We recommend Kochan’s Programming in Objective-C.

Our Teaching Philosophy

This book is based on our iPhone Development Bootcamp course. It will teach you the essential concepts of iPhone programming. At the same time, you’ll type in a lot of code and build a bunch of applications. By the end of the book, you’ll have knowledge and experience. However, all the knowledge shouldn’t (and, in this book, won’t) come first. That’s sort of the traditional way we’ve all come to know and hate. Instead, we take a learn-while-doing approach. Development concepts and actual coding go together.

Here’s what we’ve learned over the years of teaching iPhone programming:

• We’ve learned what ideas people must have to get started programming, and we focus on that subset.

• We’ve learned that people learn best when these concepts are introduced as they are needed.

• We’ve learned that programming knowledge and experience grow best when they grow together.

• We’ve learned that “going through the motions” is much more important than it sounds. Many times we’ll ask you to start typing in code before you understand it. We get that you may feel like a trained monkey typing in a bunch of code that you don’t fully grasp. But the best way to learn coding is to find and fix your typos. Far from being a drag, this basic debugging is where you really learn the ins and outs of the code. That’s why we encourage you to type in the code yourself. You could just download it, but copying and pasting is not programming. We want better for you and your skills.

What does this mean for you, the reader? To learn this way takes some trust. And we appreciate yours. It also takes patience. As we lead you through these chapters, we will try to keep you comfortable and tell you what’s happening. However, there will be times when you’ll have to take our word for it. (If you think this will bug you, keep reading — we’ve got ideas that might help.) Don’t get discouraged if you run across a concept that you don’t understand right away. Remember that we’re intentionally not providing all the knowledge you will ever need all at once. If a concept seems unclear, we will likely discuss it in more detail later when it becomes necessary. And some things that aren’t clear at the beginning will suddenly make sense when you implement them the first (or the twelfth) time.

People learn differently. It’s possible that you will love how we hand out concepts on an as-needed basis. It’s also possible that you’ll find it frustrating. In case of the latter, here are some options:

1. Take a deep breath and wait it out. We’ll get there, and so will you.

2. Check the index. We’ll let it slide if you look ahead and read through a more advanced discussion that occurs later in the book.

3. Check the online Apple documentation. This is an essential developer tool, and you’ll want plenty of practice using it. Consult it early and often.

4. If it’s Objective-C or object-oriented programming concepts that are giving you a hard time (or if you think they will), try Kochan’s Programming in Objective-C. It’s a great book that presents these concepts in a more traditional way.

How To Use This Book

This book is based on the class we teach at Big Nerd Ranch. As such, it was designed to be consumed in a certain manner.

Set yourself a reasonable goal, like “I will do one chapter every day.” When you sit down to attack a chapter, find a quiet place where you won’t be interrupted for at least an hour. Shut down your email, your Twitter client, and your chat program. This is not a time for multi-tasking; you will need to concentrate.

Do the actual programming. You can read through a chapter first, if you’d like. But the real learning comes when you sit down and code as you go. You will not really understand the idea until you have written a program that uses it and, perhaps more importantly, debugged that program.

A couple of the exercises require supporting files. For example, the SQLite exercise is a lot more fun if you have some data to browse. Thus, we have made a script that inserts data into a SQLite file. You can download these resources and solutions to the exercises from http://www.bignerdranch.com/solutions/iPhoneProgramming.zip.

There are two types of learning. When you learn about the Civil War, you are simply adding details to a scaffolding of ideas that you already understand. This is what we will call “Easy Learning”. Yes, learning about the Civil War can take a long time, but you are seldom flummoxed by it. Learning iPhone programming, on the other hand, is “Hard Learning,” and you may find yourself quite baffled at times, especially in the first few days. In writing this book, we have tried to create an experience that will ease you over the bumps in the learning curve. Here are two things you can do to make the journey easier:

• Find someone who already knows iPhone programming and will answer your questions. In particular, getting your application onto the device the first time is usually very frustrating if you are doing it without the help of an experienced developer.

• Get enough sleep. Sleepy people don’t remember what they have learned.

How This Book Is Organized

In this book, each chapter addresses one or more ideas of iPhone development followed by hands-on practice. For more coding practice, we issue challenges towards the end of each chapter. We encourage you to take on at least some of these. They are excellent for firming up the concepts introduced in the chapter and making you a more confident iPhone programmer. Finally, most chapters conclude with one or two “For the More Curious” sections that explain certain consequences of the concepts that were introduced earlier.

Chapter 1 introduces you to iPhone programming as you build and deploy a tiny application. You’ll get your feet wet with Xcode, Interface Builder, and the iPhone simulator along with all the steps for creating projects and files. The chapter includes a discussion of Model-View-Controller and how it relates to iPhone development.

Chapters 2 and 3 provide an overview of Objective-C and memory management. Although you won’t create an iPhone application in these two chapters, you will build and debug a tool called RandomPossessions to ground you in these concepts. (You will reuse this tool and its related class in the Homepwner application introduced in Chapter 10.)

In Chapters 4 and 5, you will learn about the Core Location and MapKit frameworks and create a mapping application called Whereami. You will also get plenty of experience with the important design pattern of delegation and working with protocols, frameworks, and object diagrams.

Chapters 6 and 7 focus on the iPhone user interface with the Hypnosister and HypnoTime applications. You will get lots of practice working with views and view controllers as well as implementing scrolling, zooming, paging, and navigating between screens.

Chapter 8 covers the iPhone’s accelerometer. You will learn how to obtain, filter, and use the data from the accelerometer to handle motion events, including shakes. You will use accelerometer data to add a new feature to the HypnoTime application

In Chapter 9, you will create a smaller application named HeavyRotation while learning about UIDevice notifications and how to implement autorotation in an application.

Chapter 10 introduces the largest application in the book – Homepwner. (By the way, “Homepwner” is not a typo; you can find the definition of “pwn” at www.urbandictionary.com.) This application keeps a record of your possessions in case of fire or another catastrophe. Homepwner will take nine chapters total to complete.

In Chapters 10, 11, and 16, you will build experience developing tables on the iPhone. You will learn about table views, their view controllers, and their data sources. You will learn how to display data in a table, how to allow the user to edit the table, and how to improve the interface.

Chapter 12 builds on the navigation experience gained in Chapter 7. You will learn how to use UINavigationController, and you will give Homepwner a drill-down interface and a navigation bar.

In Chapter 13, you’ll learn how to take pictures with the iPhone’s camera and how to display and store images in Homepwner. You’ll use NSDictionary and UIImagePickerController You’ll also learn about UIPopoverController for the iPad

Chapter 14 delves into ways to save and load data. In particular, you will archive data in the Homepwner application using the NSCoding protocol. The chapter also shows you how to work with multitasking and transistions between application states, such as active, background, and suspended.

Chapter 15 teaches you how to prepare for low-memory warnings and leads you through handling low-memory warnings in Homepwner.

In Chapter 17, you’ll take a break from Homepwner and create a drawing application named TouchTracker. You’ll learn how to add multi-touch capability and more about touch events. You’ll also get experience with the first responder and responder chain concepts and more practice with NSDictionary. In addition, you’ll learn about the Instruments application while debugging performance and memory issues in TouchTracker.

Chapters 18 and 19 introduce layers and the Core Animation framework with a brief return to the HypnoTime application to implement animations. You will learn about implicit and explicit animations and animation objects, like CABasicAnimation and CAKeyFrameAnimation.

Chapter 20 will teach you how to play audio and video while building an application called MediaPlayer. You will learn about playing audio and video on the iPhone, where to keep these resources, streaming limits, and the low-level audio API. You will also enable MediaPlayer to play music while in the background state and learn guidelines and other uses for background execution.

Chapter 21 ventures into the wide world of web services. You will fetch and parse XML data from the iTunes server in an application you create named TopSongs. You’ll use NSURLConnection and NSXMLParser along the way.

In Chapter 22, you’ll return to Homepwner to learn about the iPhone’s Address Book functions and the People Picker as you update Homepwner to allow the user to assign people to inherit possessions.

Chapter 23 introduces the concepts and techniques of internationalization and localization. You will learn about NSLocale, strings tables, and NSBundle as you localize Homepwner.

Chapter 24 teaches you how to publish a service on the peer-to-peer network Bonjour. You will start a new application named Nayberz that advertises itself on the network.

Chapter 25 explores how to get an application to work with the iPhone’s Settings application to create application settings and preferences that the user can customize. You will use NSUserDefaults and give Nayberz a pane in Settings.

Chapter 26 introduces the SQLite library for storing and fetching data on the iPhone. You get a chance to practice with a small data application named Nayshunz.

Chapter 27 gives you a good grounding in using Core Data to store and access data in an iPhone application. In this chapter, you will build a complex and business-like one named Inventory.

Chapter 28 introduces the iPad and some of its features, like UIGestureRecognizer and Core Text. You will turn the Whereami application into a universal application, enabling it to run natively on the iPad and the iPhone.

It is important to note something that is not covered in this book: OpenGL ES. We actually wrote a chapter. And then we rewrote it. And rewrote it. And rewrote it. It got longer with every pass. Thus, we’ve decided to take that chapter and expand it into a separate book.

Style Choices

This book contains a lot of code. We have attempted to make that code and the designs behind it exemplary. We have done our best to follow the idioms of the community, but at times we have wandered from what you might see in Apple’s sample code or code you might find in other books. You may not understand these points now, but it is best that we spell them out before you commit to reading this book:

• There is an alternative syntax for calling accessor methods and it is known as dot-notation. In this book, we will explain dot-notation, but we will not use it. For us and most beginners, dot-notation tends to obfuscate what is really happening.

• In our subclasses of UIViewController, we always change the designated initializer to init. It is our opinion that the creator of the instance should not need to know the name of the NIB file that the view controller uses, or even if it has a NIB file at all.

• We will always create view controllers programmatically. Some programmers will instantiate view controllers inside XIB files. We’ve found this practice leads to projects that are difficult to comprehend and debug.

• We will nearly always start a project with the simplest template project: the window-based application. The boilerplate code in the other template projects doesn’t follow the rules that procede this one, so we think they make a poor basis upon which to build.

We believe that following these rules makes our code easier to understand and easier to maintain. After you have worked through this book (where you will do it our way), you should try breaking the rules to see if we’re wrong.

Typographical Conventions

To make this book easier to read, certain items appear in certain fonts. Class names, method names, and function names appear in a bold, fixed-width font. Class names start with capital letters, and method names start with lowercase letters. In this book, method and function names will be formatted the same for simplicity’s sake. For example, “In the loadView method of the RexViewController class, use the NSLog function to print the value to the console.”

Variables, constants, and types appear in a fixed-width font but are not bold. So you’ll see, “The variable fido will be of type float. Initialize it to M_PI.”

Applications and menu choices appear in the Mac system font. For example, “Open XCode and select New Project... from the File menu.”

All code blocks will be in a fixed-width font. Code that you need to type in is always bold. For example, in the following code, you would type in everything but the first and last lines. (Those lines are already in the code and appear here to let you know where to add the new stuff.)

image

Hardware, Software, and Deployment

To develop iPhone applications, you will need an Intel Mac running Mac OS X Leopard (or above). You will also need to download the iPhone SDK (Software Development Kit). The SDK includes Xcode (Apple’s Integrated Development Environment), the iPhone simulator, and other development tools. To download the iPhone SDK, you only need to register as an iPhone Developer, which is free. As a “Registered iPhone Developer,” you will be able to access the iPhone Dev Center (including the Development docs). Go to http://developer.apple.com/iphone/program/download.html to register. Make sure you have the USB cable that connects the device to the computer.

You can do a lot with just the simulator, but for more complete and realistic testing, you’ll want to install your applications on a real device. device — an iPhone, iPod, or iPod touch. (Nearly everything is this book will apply to all three devices, but we will usually refer to the “iPhone.” The iPad runs the same OS as the iPhone, and writing iPad applications uses the same techiniques with a few additions discussed in the the final chapter. The iPod touch is nearly the same as the iPhone except for the telephone.) To install applications To install applications on your iPhone or to distribute them on the App Store, you have to join the “iPhone Developer Program,” which costs $99/year. Go to http://developer.apple.com/ to join.

Excited yet? Good. Let’s get started.

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

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