Introduction
Apple has a history of alternating its releases between user-focus and developer-focus. The good news about iOS 5 is that it’s all about the developers. The addition of Automatic Reference Counting (ARC) alone is worth the upgrade for developers. In one move, Apple has eliminated the number one cause of crashes in iOS applications, while making the code easier to write and faster to run. Moving to ARC is the single best thing you can do for your application. It’s the most important Objective-C feature since the autorelease pool.
But iOS 5 adds many more features for the developer. From iCloud to automatic data protection, the operating system now takes care of more of the hard problems, letting developers focus on making the best apps.
Most visible to developers is the new Xcode. Some of it is better, some of it is just different, and some of it will make you crazy. It’s the new game in town, though, and everyone needs to get used to it. This book will help you figure it out.
If you’re ready to take on the newest Apple release and push your application to the limits, this is the book to get you there.
Who This Book Is For
This is not an introductory book. There are many books out there that will teach you Objective-C and take you step by step through Interface Builder. This is not that book. This book assumes that you have a little experience with iOS. Maybe you’re self-taught, or maybe you’ve taken a class. You’ve hopefully written at least most of an application, even if you haven’t submitted it yet. If you’re ready to move beyond the basics, to learn the best practices and the secrets that the authors have learned from practical experience writing real applications, then this is the book for you.
This book also is not just a list of recipes. There’s plenty of sample code here, but the focus is on learning how to design, code, and maintain great iOS apps. A lot of this book is about why rather than just how. You’ll learn about as much about design patterns and writing reusable code as about syntax and new frameworks.
All the examples use Xcode 4. If you’re not comfortable with Xcode 4 yet, don’t worry. Chapter 2 is devoted to getting you up to speed.
What This Book Covers
The iOS platforms always move forward, and so does this book. Most of the examples here require iOS 5. All examples use Automatic Reference Counting. Except in a very few places, this book will not cover backward compatibility. If you’ve been shipping code long enough to need backward compatibility, you probably know how to deal with it. This book is about writing the best-possible apps using the best features available.
This book focuses on the iPhone 4 and iPad 2. Most topics here are applicable to the original iPad, iPod touch, iPhone 3GS, and Apple TV. At the time of writing the iPhone 5 and iPad 3 have not been released, but everything here should apply to them as well. Chapter 12 is devoted to dealing with the differences between the platforms.
How This Book Is Structured
iOS has an extremely rich set of tools, from high-level frameworks like UIKit to very low-level tools like Core Text. Often, there are several ways to achieve a goal. As a developer, how do you pick the right tool for the job?
This book separates the everyday from the special purpose, helping you pick the right solution to each problem. You’ll learn why each framework exists, how the frameworks relate to each other, and when to choose one over another. Then you’ll learn how to make the most of each framework for solving its type of problem.
There are four parts to this book, moving from the most common tools to the most powerful:
Part I: What’s New?
If you’re familiar with iOS 4, then this section quickly introduces you to the new features of iOS 5.
■ Chapter 1: The Brand New Stuff — iOS adds a lot of new features, and here you get a quick overview of what’s available.
■ Chapter 2: Getting Comfortable with Xcode 4 — Apple recently redesigned the Xcode interface, and it can take some getting used to. This chapter shows you how to get the most out of it.
Part II: Getting the Most Out of Everyday Tools
As an iOS developer, you’ve encountered a wide variety of common tools, from notifications to table views to animation layers. But are you using these tools to their full potential? In this part, you learn the best practices in Cocoa development from seasoned developers.
■ Chapter 3: Everyday Objective-C—If you’re ready to move to the next level in Objective-C, this chapter introduces you to the tools experienced developers use every day to improve application design, maintainability, and reusability.
■ Chapter 4: Hold On Loosely: Cocoa Design Patterns—Cocoa relies on a number of common and consistent design patterns. You learn what they are so you can solve problems the same way Apple does.
■ Chapter 5: Getting Table Views Right—Table views are perhaps the most complex and commonly used UI element in iOS. They are simple and elegant in design, but confusing to developers who don’t understand how they work. You learn how to use them correctly and to solve some special problems like infinite scrolling.
■ Chapter 6: Better Drawing—Custom drawing is intimidating to many new developers, but it’s a key part of building beautiful and fast user interfaces. You’ll discover the available drawing options from UIKit to Core Graphics, and how to optimize them to look their best while keeping them fast.
■ Chapter 7: Layers Like an Onion: Core Animation—iOS devices have incredible facilities for animation. With a powerful GPU and the highly optimized Core Animation, you can build engaging, exciting, and intuitive interfaces. In this chapter, you go beyond the basics and learn the secrets of animation.
■ Chapter 8: Tackling Those Pesky Errors—You try to write perfect code, but sometimes things go wrong. How your application reacts to the unexpected is what separates decent apps from extraordinary apps. You’ll learn the common patterns for error handling, how to log, and how to make your code more resilient against the unexpected.
Part III: The Right Tool for the Job
There are tools that are part of nearly every application, and there are tools that you only need from time to time. In this section, you learn about those tools and techniques that are a little more specialized.
■ Chapter 9: Controlling Multitasking—Multitasking is an important part of many applications, and you learn how to do multiple things at once while your application is running and when your application is in the background.
■ Chapter 10: REST for the Weary—REST-based services are a mainstay of modern applications, and you learn how to best implement them in iOS.
■ Chapter 11: Batten the Hatches with Security Services—User security and privacy are paramount today, and you learn how to protect your application and user data from attackers with the keychain, certificates, and encryption.
■ Chapter 12: Running on Multiple iPlatforms and iDevices—The iOS landscape gets more complex every year with iPod touch, iPhone, iPad, Apple TV, and a steady stream of new editions. It’s not enough just to write once, run everywhere. You need your applications to be their best everywhere. You’ll learn how to adapt your apps to the hardware and get the most out of every platform.
■ Chapter 13: Internationalization and Localization—While you may want to focus on a single market today, there are small things you can do to ease the transition to a global market tomorrow. Save money and headaches later, without interrupting today’s development.
■ Chapter 14: Selling Past the Sale with In App Purchases—In App Purchases are still an untapped market for many developers. Users like the add-on content, and developers love the extra revenue. You learn the best ways to make this important feature a reality in your application.
Part IV: Pushing the Limits
This section is what this book is all about. You’ve learned the basics. You’ve learned the everyday. Now push the limits with the most advanced tools available. You learn the ins and outs of deep iOS.
■ Chapter 15: Cocoa’s Biggest Trick: Key-Value Observing—Many of Apple’s most powerful frameworks rely on KVO for their performance and flexibility. You learn how to leverage the flexibility and speed of KVO, as well as the trick that makes it so transparent.
■ Chapter 16: Think Different: Blocks and Functional Programming—Many developers are still absorbing the addition of blocks to Objective-C. They’re valuable for interacting with Apple frameworks, but they also open new ways of thinking about your program. Embrace a new style, and maximize its benefits in your next project.
■ Chapter 17: Going Offline—Network programming is hard, but even harder is providing a seamless offline experience. Learn how to best cache your data and integrate it into your network engine.
■ Chapter 18: Fancy Text Layout—From UIKit to Core Text, iOS is full of ways to display text. There’s no perfect solution for displaying rich text in iOS, so it’s important to learn the trade-offs so you can choose the right solution and use it correctly.
■ Chapter 19: Building a (Core) Foundation—When you want the most powerful frameworks available on iOS, you’re going to want the Core frameworks like Core Graphics, Core Animation, and Core Text. All of these rely on Core Foundation. In this chapter you learn how to work Core Foundation data types so you can leverage everything iOS has to offer.
■ Chapter 20: Deep Objective-C—When you’re ready to pull back the curtain on how Objective-C really works, this is the chapter for you. You learn how to use the Objective-C runtime directly to dynamically modify classes and methods. You also learn how Objective-C method calls are dispatched to C function calls, and how you can take control of the system to extend your programs in incredible ways.
You can skip around in this book to focus on the topics you need most. Each chapter stands alone, except for those that require Core Foundation data objects (particularly Core Graphics, Core Animation, and Core Text). Those chapters direct you to Chapter 19, “Building a (Core) Foundation,” when you need that information.
What You Need to Use This Book
All examples in this book were developed with Xcode 4.2 on Mac OS X 10.7 and iOS 5. You need an Apple developer account to access most of the tools and documentation, and you need a developer license to run applications on your iOS device. Visit to sign up.
Most of the examples in this book will run in the iOS Simulator that comes with Xcode 4.2. You can use the iOS Simulator without an Apple developer license.
There are few differences between Xcode 4.2 on Mac OS X 10.6 and 10.7, so all examples should work under 10.6.
Finding Apple Documentation
Apple provides extensive documentation at its website and within Xcode. The URLs change frequently and are often very long. This book refers to Apple documents by title rather than by URL. To find documents in Xcode, press Cmd-Option-? or click Help → Documentation and API Reference. In the Documentation Organizer, click the Search icon, type in the name of the document, and then select the document from the search results. See Figure 1 for an example of how to search for the Coding Guidelines for Cocoa.

Figure 1 Searching for Coding Guidelines for Cocoa
To find documents at the Apple developer site, visit , click Member Center and log in. Select the iOS Dev Center, and enter the document title in the Search Developer search box.
The online documentation is generally identical to the Xcode documentation. You may receive results for both iOS and Mac. Make sure to choose the iOS version. Many iOS documents are copies of their Mac counterparts, and occasionally include function calls or constants that are not available on iOS. This book guides you about which features are available on iOS.
Source Code
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All of the source code used in this book is available for download at . For example, you will find the following sample code online in the Chapter 18 folder, in the SimpleLayout
project, and the CoreTextLabel.m
file:
CoreTextLabel.m (SimpleLayout)
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CGAffineTransform
transform = CGAffineTransformMakeScale(1, -1);
CGAffineTransformTranslate(transform,
0, -self.bounds.size.height);
self.transform = transform;
self.backgroundColor = [UIColor whiteColor];
}
return self;
}
Some source code snippets shown in the book are not comprehensive and are meant to help you understand the chapter. For those instances, you should refer to the files available on the website for the complete source code.