A comprehensive introduction to creating Appsfor iPhone, iPad and iPod touch Beginning iPhone 4 Development Exploring the iOS SDK Cov ers iPad and iPod t ouch Companion eBook Available
Trang 1A comprehensive introduction to creating Apps
for iPhone, iPad and iPod touch
Beginning
iPhone 4 Development
Exploring the iOS SDK
Cov ers
iPad and iPod t ouch
Companion
eBook Available
Trim: 7.5 x 9.25 spine =1.28125" 680 page count 534ppi
COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION
US $39.99
Shelve in Mobile Computing User level:
Beginner–Intermediate
www.apress.com
http://iphonedevbook.com/forum/
SOURCE CODE ONLINE
BOOKS FOR PROFESSIONALS BY PROFESSIONALS®
ISBN 978-1-4302-3024-3
9781430 230243
5 39 9 9
this print for content only—size & color not accurate
CYAN
MAGENTA YELLOW BLACK
PANTONE 123 C
SPOT MATTE
The authors of the bestselling Beginning iPhone 3 Development are back,
with more excellent material completely updated from the ground up for iOS 4 and the latest version of Apple¹s Xcode 3 All source code has been re-worked to use the latest Xcode templates and current APIs, with brand new screenshots showing iOS 4 and Xcode 3 in action
Beginning iPhone 4 Development is a complete course in iOS development You’ll
master techniques that work on iPhone, iPad, and iPod touch We start with the basics, showing you how to download and install the tools you’ll need, and how
to create your first simple application Next you’ll learn to integrate all the inter-face elements iOS users have come to know and love, such as buttons, switches, pickers, toolbars, and sliders You’ll master a variety of design patterns, from the simplest single view to complex hierarchical drill-downs The confusing art of table building will be demystified, and you’ll learn techniques to save and re-trieve your data using SQLite, iPhone’s built-in database management system and Core Data, the standard for persistence that Apple brought to iOS with the release of SDK 3
And there’s much more! You’ll learn to draw using Quartz 2D and OpenGL ES, add multitouch gestural support (pinches and swipes) to your applications, and work with the camera, photo library, accelerometer, and built-in GPS You’ll dis-cover the fine points of application preferences and learn how to localize your apps for multiple languages You’ll also learn how to use the new
concurren-cy APIs included in iOS 4, and make robust multithreaded applications using Grand Central Dispatch
for Cocoa Touch developers
Trang 21
Welcome to the Jungle
So, you want to write iPhone, iPod touch, and iPad applications? Well, we can’t say that
we blame you iOS, the core software of all of these devices, 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 iOS applications And with the release of
version 4 of the iOS software development kit (SDK), things have only gotten better
What This Book Is
This book is a guide to help you get started down the path to creating your own iOS
applications Our goal is to get you past the initial learning curve to help you understand
the way iOS 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 iOS 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 need to build your own
professional iPhone and iPad applications
TIP: Dave, Jack, 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 writing software for iOS, you’ll need a few things For starters,
you’ll need an Intel-based Macintosh running Snow Leopard (OS X 10.6.5 or later) Any
recent Intel-based Macintosh computer—laptop or desktop—should work just fine
1
Trang 3CHAPTER 1: Welcome to the Jungle
2
You’ll also need to sign up to become a registered iOS developer Apple requires this step before you’re allowed to download the iOS SDK
To sign up, navigate to http://developer.apple.com/ios/, which will bring you to a page similar to the one shown in Figure 1–1
Figure 1–1 Apple’s iOS Dev Center web site
Trang 4First, click the button labeled Log in You’ll be prompted for your Apple ID 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 iOS development page Not only will you
see 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 iOS
application development
One of the most important downloads on this page 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!
SDK VERSIONS AND SOURCE CODE FOR THE EXAMPLES
As the versions of the SDK and Xcode evolve, the mechanism for downloading them will also change
Sometimes the SDK and Xcode are featured as separate downloads; other times, they will be merged as a
single download Bottom line: you want to download the latest released (nonbeta) version of the SDK and
Xcode
This book has been written to work with the latest version of the SDK In some places, we have chosen to
use new functions or methods introduced with version 4 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’ll update the code as new versions of the SDK are released, so be sure to check the site periodically
Developer Options
The free SDK download option includes a simulator that will allow you to build and run
iPhone and iPad apps on your Mac This is perfect for learning how to program for iOS
However, the simulator does not support hardware-dependent features, such as the
iPhone’s accelerometer or camera Also, the free option will not allow you to download
your applications onto your actual iPhone or other device And it does not give you the
ability to distribute your applications on Apple’s App Store For those capabilities, you’ll
need to sign up for one of the other options, which aren’t free:
The Standard program costs $99/year It provides a host of
development tools and resources, technical support, distribution of
your application via Apple’s App Store, and, most important, the ability
to test and debug your code on an iOS device, rather than just in the
simulator
The Enterprise program costs $299/year It is designed for companies
developing proprietary, in-house iOS applications and for those
developing applications for the Apple’s App Store, and with more than
one developer working on the project
Trang 5CHAPTER 1: Welcome to the Jungle
4
For more details on these programs, visit http://developer.apple.com/programs/ios
and http://developer.apple.com/programs/ios/enterprise to compare the two Because iOS supports an always-connected mobile device that uses other companies’ wireless infrastructure, Apple has needed to place far more restrictions on iOS
developers than it ever has on Mac developers (who are able—at least as of this
writing—to write and distribute programs with absolutely no oversight or approval from Apple) Even though the iPod touch and the Wi-Fi–only versions of the iPad don’t use anyone else’s infrastructure, they’re still subject to these same restrictions
Apple has not added restrictions to be mean, but rather as an attempt to minimize the chances of malicious or poorly written programs being distributed that could degrade performance on the shared network Developing for iOS may seem like it presents a lot
of hoops to jump through, but Apple has expended quite an effort to make the process
as painless as possible And also consider 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, iPod touch, or iPad While much
of your code can be tested using the iOS simulator, not all programs can be And even those that can run on the simulator really need to be thoroughly tested on an actual device 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 an actual device 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 iOS
simulator
What You Need to Know
This book assumes that you already have some programming knowledge It assumes that you understand the fundamentals of object-oriented programming (you know 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 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 iOS itself, as a user Just as you would with any
platform for which you wanted to write an application, get to know the iPhone, iPad, or iPod touch’s nuances and quirks Take the time to get familiar with the iOS interface and with the way Apple’s iPhone and/or iPad applications look and feel
Trang 6xv
Acknowledgments
This book could not have been written without our mighty, kind, and clever families, friends, and
cohorts First and foremost, eternal thanks to Terry, Weronica, and Deneen for putting up with
us, and for keeping the rest of the universe at bay while we toiled away on this book This project
saw us tucked away in our writers’ cubby for many long hours, and somehow, you didn’t
complain once We are lucky men
This book could not have been written without the fine folks at Apress Clay Andres brought us to
Apress in the first place and carried this book on his back Dominic Shakeshaft was the gracious
mastermind who dealt with all of our complaints with a smile on his face, and somehow found
solutions that made sense and made this book better Kelly Moritz, our wonderful and gracious
coordinating editor, was the irresistible force to our slowly movable object Tom Welsh, our
developmental editor, helped us with some terrific feedback along the way They kept the book
on the right track and always pointed in the right direction Marilyn Smith and Kim Wimpsett,
copy editors extraordinaire, you were both such a pleasure to work with! Jeffrey Pepper, Frank
McGuckin, Brigid Duffy, and the Apress production team took all these pieces and somehow
made them whole Leo Cuellar, Dylan Wooters, and Jeff Stonefield assembled the marketing
message and got it out to the world To all the folks at Apress, thank you, thank you, thank you!
A very special shout-out to our incredibly talented technical reviewer, Mark Dalrymple In
addition to providing insightful feedback, Mark tested all the code in this book, and he helped
keep us on the straight and narrow Thanks, Mark!
Finally, thanks to our children for their patience while their dads were working so hard This book
is for you, Maddie, Gwynnie, Ian, Kai, Henrietta, Dorotea, Daniel, Kelley, and Ryan
Trang 7■ ACKNOWLEDGMENTS
xvi
Preface
The preface to the previous edition of this book, Beginning iPhone 3 Development, started with
the phrase, “What an amazing journey!” Well, it’s true We’re having a blast, making a lot of new friends, and above all, learning, learning, learning The iOS SDK continues to evolve, and with each new release, it brings new concepts to explore and new design patterns to master
So, what’s new with this edition? A lot! For starters, we’ve gone through every single line of code and made whatever changes were necessary to bring each project into line with the latest version of iOS and the latest released SDK All the projects were created with Xcode 3.2.5, the newest release version of Xcode As you would expect, we’ve gone through the text, too, so the explanations are all up to date
Is it worth buying this book if you already own an earlier edition? That is an excellent question We’ve made a lot of minor tweaks throughout the book, but we’ve also made many major changes We’ve added two new chapters: one on programming for iPad, and a second on threading and background processing Both of these chapters feature all new code and are designed with iOS 4 in mind
In short, we’ve definitely made the book a lot better If you’ve already been through the
previous edition and feel very comfortable with all the material, go ahead and move on to More iPhone 4 Development, which we’re working on as this book goes to press
If you haven’t made it through the entire first or second edition yet, if you feel a bit fuzzy still,
or if you just want to help us out as authors, then by all means, pick up this third edition We do appreciate your support Be sure to check out http://iphonedevbook.com, and drop us a line
to let us know about your amazing new apps We look forward to seeing you on the forum Happy coding!
Dave, Jack, and Jeff