Preface

So you want to write applications for the iPhone. The first thing you should know is that—even after the initial release of the Apple SDK—the iPhone is by and large a closed device, and Apple has taken steps to lock developers out of the operating system. This hasn’t stopped people from plumbing the iPhone’s depths and creating applications. Since Apple’s initial release of the iPhone, the popular device has been touted as one of the most heavily hacked mobile devices in existence. As early as fall 2007, Nicholas Penree’s http://jailbreakme.com logged that over 30 percent of the market had broken into their devices to run third-party, unsanctioned applications on their iPhones, and the community has since been growing steadily.

A team of iPhone hackers, known as the iPhone Dev team, reported over six million page hits on their website within the first 30 days of the release of the iPhone 3G. Most other sites hosting iPhone hacking tools have similarly reported record traffic, and even Apple’s own employees have been caught traipsing around the Genius Bar touting their “customized” devices.

Within only a few months of the iPhone’s initial release, the open source community had accomplished an amazing feat that caused a considerable disturbance to Apple: the community designed a free, open source compiler and tool chain for building iPhone applications, deployed an over-the-air community distribution repository, and built a notably large developer base. All of this was accomplished on a device that was intended to be closed. In 2008, it became apparent that an iPhone software development community had not only emerged, but was flourishing in designing stellar third-party applications for the popular device—and without any help from Apple. With the sizable potential for revenue obvious, Apple sought to deploy their own version of what the open source community had already built: a compiler and tool chain, over-the-air distribution repository, and large developer base. Whether this was the result of pressure from the community can only be speculated, but one thing is for certain: the open development community proved it could be done successfully.

In March 2008, the Apple SDK was introduced. The SDK allows developers to write applications using Apple-sanctioned tools and rewards them with the potential of selling to a very large audience through Apple’s online market, the AppStore. Many developers have found, however, that the trade-off is considerable. Applications run in a sandbox, and are not allowed to utilize many components of the operating system. This has prevented applications from performing simple tasks such as running in the background, effectively killing instant messaging applications and any other real-time background utilities. The arbitrary restrictions in the Apple SDK have also prevented developers from writing more complex applications, such as movie players, high performance 2D games, and any type of application that Apple believes could compete with their own suite of software.

In addition to refusing access to several known, valuable features of the iPhone, the SDK introduced a set of new, more restrictive objects which “sheltered” many more low-level APIs from the developer. Although these APIs continue to exist on the iPhone platform (and have, in fact, been shown to be used by many of Apple’s own applications), the restriction of these interfaces has given what many consider an unfair advantage to Apple. To add to developers’ complaints, Apple’s developer agreement has been criticized for restricting certain types of applications, including turn-by-turn navigation software, and bytecode interpreters (such as Java and Flash).

Citing all of these concerns, many developers have chosen to join the ranks of those writing open software that is free from Apple’s licensing and technical restrictions, software that truly uses the same APIs as Apple’s own applications, and is capable of accessing features at a much lower level than run-of-the-mill AppStore software. Today, you’ll find many great applications exclusively in the open development community. The community software installer, Cydia, distributes many great applications, such as game emulators, video players, Unix tools, theme managers, and other great software. The open development community has developed its own convenient tools because developers have found the Apple SDK to be too technically or politically limiting. Many excellent commercial packages outside the AppStore also outperform their AppStore counterparts, simply because they have unfettered access to the iPhone’s low-level APIs. Even a simple flashlight application can outperform AppStore flashlights, simply because it can adjust screen brightness. Finally, you’ll discover a level of stability in the open source tool chain that has provided considerable code compatibility across major firmware revisions—an area of significant frustration to Apple SDK developers who have fallen victim to frequent code rewrites.

Thus, with tool chain in hand and many sleepless nights of tomhackery, the community has learned how to use the low-level frameworks and interfaces on the iPhone to design spectacular third-party applications that have the potential to give the AppStore a run for its money. This book walks through the frameworks that are key to designing full-featured software on the iPhone, with pointers to tools that are available to take advantage of the other frameworks not documented here.

Although this book covers the open development community’s compiler and tools, and not specifically the Apple SDK, developers using the Apple SDK will find that much of this book overlaps with objects either directly or indirectly available in the SDK, and that some technical limitations can be worked around to take advantage of certain functionality. Be warned, however, that many APIs discussed here are officially restricted from use in the SDK. This book is really designed for the open developers among us.

The iPhone is a superb device, and in spite of the politics surrounding its availability to developers, the community is growing fast. With or without Apple’s help, the iPhone is giving birth to many new commercial markets for itself and will soon grow beyond the success of its predecessors: the PocketPCs and the Symbians that formerly owned the mobile market space.

As you read this book, you probably won’t realize just how good you’ve got it. The simplicity you’ll see in this book reflects thousands of hours of work by the active development community, chipping away at the nearly impossible challenges that were involved in this task. The old-school methods for getting anything done on the iPhone were laborious, if not exhausting, and could have filled up volumes of books alone.

Work continues today to discover many of the proprietary interfaces on the iPhone, and we welcome anyone into the community with the know-how and perseverance to join us in this endeavor.

Audience for This Book

You’ll need some prior knowledge of coding to find this book useful. The iPhone framework uses Objective-C, which we’ll introduce you to in Chapter 2. The good news is that you can also use C and C++ in your applications, so anyone with pre-existing knowledge should be able to pick it up pretty quickly. If you don’t understand C or C++, there are many books available on the subject. This book isn’t a primer for any of these languages, but rather is designed to explain the proprietary classes and methods needed to write iPhone-specific applications.

Organization of the Material

Chapter 1, Breaking Into and Setting Up the iPhone, explains how to break into your iPhone.

Chapter 2, Getting Started with Applications, illustrates the makeup of an iPhone application and how to get the tool chain running on your desktop.

Chapter 3, Introduction to UIKit, introduces you to UIKit, which is at the core of developing iPhone applications and user interfaces.

Chapter 4, Event Handling and Graphics Services, covers basic geometric concepts as used in the Core Graphics framework and event notifications.

Chapter 5, Advanced Graphics Programming with Core Surface and Quartz Core, goes deeper into iPhone development by exploring raw video surfaces and 3-D transformations.

Chapter 6, Making Some Noise, covers the many different ways to record and play sounds and output a digital audio stream.

Chapter 7, Advanced UIKit Design, illustrates many of the advanced user interface components of UIKit.

The Appendix highlights many miscellaneous hacks and open source classes so you can do cool things in your iPhone application.

Conventions Used in This Book

The following typographical conventions are used in this book:

Plain text

Used for menu titles, menu options, menu buttons, and keyboard accelerators.

Italic

Indicates new terms, URLs, filenames, Unix utilities, and command-line options.

Constant width

Indicates the contents of files, the output from commands, variables, types, classes, namespaces, methods, values, objects, and generally anything found in programs.

Constant width bold

Shows commands or other text that should be typed literally by the user, and parts of code or files highlighted to stand out for discussion.

Constant width italic

Shows text that should be replaced with user-supplied values.

Tip

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “iPhone Open Application Development, Second Edition, by Jonathan Zdziarski. Copyright 2009 Jonathan Zdziarski, 978-0-596-15519-3.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

Safari® Books Online

Note

When you see a Safari® Books Online icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf.

Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.

The technologies discussed in this publication, the limitations on these technologies that the technology and content owners seek to impose, and the laws actually limiting the use of these technologies are constantly changing. Thus, some of the projects described in this publication may not work, may cause unintended harm to equipment or systems on which they are used, or may be inconsistent with applicable law or user agreements. Your use of these projects is at your own risk, and O’Reilly Media, Inc. disclaims responsibility for any damage or expense resulting from their use. In any event, you should take care that your use of these projects does not violate any applicable laws, including copyright laws.

We’d Like to Hear from You

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/9780596155193/

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at:

http://www.oreilly.com

Acknowledgments

Special thanks to Patrick Walton, Jay Freeman, Brian Whitman, John Bafford, Nicholas Penree, Elliot Kroo, Dino Pastos, Nate True, Steve Dunham, Nicolas Bacca, Daniel Peebles, Alexander Pick, Aaron Alexander, Richard Thally, Justin Lazarow, Chris Zimman, Eric McDonald, and many others wishing to remain anonymous in the iPhone development community who have contributed sleepless nights, great humor, and cash from their own pockets to open the iPhone and build a solid foundation for application development.

Get iPhone Open Application Development, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.