Introduction

Apple has a history of alternating its releases between being user-focused and developer-focused. iOS 6 is mostly about users. There’s a totally new map interface with turn-by-turn driving directions. Social networks permeate the interface. Pass Kit opens up whole new ways to integrate the iPhone with daily life. The Siri enhancements make the iPhone 4S even more compelling. Expect users to upgrade quickly.

That’s great news, because iOS 6 is an exciting release for developers, too. So many things have gotten so much easier. It’s easier to lay out your UI with collection views and auto layout. UIKit has added rich text support. When your apps go into the background, state preservation makes it easy to keep track of where you were. Many things that were hard or tedious just became a lot simpler.

If you’re ready to take on the newest Apple release and push your application to the limits, this book will get you there.

Who This Book Is For

This is not an introductory book. Many other books out there cover Objective-C and will walk you step by step through Interface Builder. However, 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 gleaned 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 discovering how to design, code, and maintain great iOS apps. Much of this book is about why rather than just how. You’ll find out as much about design patterns and writing reusable code as about syntax and new frameworks.

What This Book Covers

The iOS platforms always move forward, and so does this book. Most of the examples here require at least iOS 5, and many require iOS 6. All examples use Automatic Reference Counting, automatic property synthesis, and object literals. 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 iPhone 4, iPad 2, and higher models. Most topics here are applicable to the original iPad, iPod touch, iPhone 3GS, and Apple TV. Chapter 16 is devoted to dealing with the differences between the platforms.

What Is New in This Edition

This edition covers most of the newest additions to iOS 6, including collection views and Auto Layout (Chapter 7), and a complete chapter is dedicated to Automatic Reference Counting. We have added a whole new chapter explaining how to move your data to the cloud, covering some third-party backend as a service (BaaS) offerings. More relevant to iOS, this year saw the greatest improvement in the LLVM compiler and debugger. We expanded the LLVM section in Chapter 2 to highlight the important features of LLVM that you could use to write the next-generation apps. We also added a whole new chapter dedicated to debugging with LLDB (Chapter 19), covering advanced topics, including Python scripting, knowing the layout of registers, and reading the registers to solve the most difficult debugging problems. That chapter also provides a brief look at some command-line tools such as otool and atos that can help you with debugging. Lastly, Chapter 20 is dedicated to showing how to optimize your CPU performance, memory usage, network bandwidth, and battery-using Instruments.

Even if you have the previous edition of this book, the new coverage on debugging (Chapter 19), performance tuning (Chapter 20), and collection views and Auto Layout (Chapter 7) will probably be enough to convince you that you need a copy of this edition.

How This Book Is Structured

iOS has a very 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 discover 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. Chapters that are new in this edition or have been extensively updated are indicated.

Part I: What’s New?

If you’re familiar with iOS 5, then this section quickly introduces you to the new features of iOS 6.

(Updated) Chapter 1: “The Brand New Stuff”—iOS 6 adds a lot of new features, and here you get a quick overview of what’s available.

(Updated) Chapter 2: “Getting Comfortable with Xcode 4 and the LLVM compiler”—Xcode 4 is a huge IDE, and many of its features aren’t obvious. Whether you’re new to Xcode or a veteran from earlier versions, this chapter will help you make the most of this powerful (and sometimes frustrating) tool.

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 find the best practices from seasoned developers in Cocoa development.

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 discover what they are so you can solve problems the same way Apple does.

(New) Chapter 5: “Memory Management with Objective-C ARC”—Automatic Reference Counting has radically changed how iOS developers work. While most of the conversion is easy, migrating your code and your practices can create some subtle problems. You find out how to avoid the problems and get the most out of this powerful technology.

Chapter 6: “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 how to solve some special problems like infinite scrolling.

(New) Chapter 7: “Great at Any Angle: Collection Views and Auto Layout”—Nothing created as much buzz at WWDC 2012 as UICollectionView. Up until now, UITableView has dominated iOS interfaces, but that will likely change in iOS 6 as developers master this new control. With the addition of constraint-based layout from OS X, incredibly complex and beautiful interfaces are now much simpler to implement. This chapter shows you the ropes.

Chapter 8: “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 9: “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 discover the secrets of animation.

Chapter 10: “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 find the common patterns for error handling, how to log, and how to make your code more resilient against the unexpected.

(New) Chapter 11: “Location Services: Know Where You Are”—Many applications need to know where the user is. While iOS makes this easy, it is important not to drain the battery in the process. You find out how to balance functionality with efficiency and give the user the best possible experience.

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 the tools and techniques that are a little more specialized.

(New) Chapter 12: “Common UI Paradigms Using Table Views”—Exciting as collection views are, the table view is still the workhorse of iOS. You find out how to take table views beyond the basics with infinite scrolling views and other advanced topics.

(Updated) Chapter 13: “Controlling Multitasking”—Multitasking is an important part of many applications, and you discover how to do multiple things at once while your application is running and when your application is in the background. The new state preservation system of iOS 6 makes this much easier, and you learn how to use it for common situations.

(Updated) Chapter 14: “REST for the Weary”—REST-based services are a mainstay of modern applications, and you learn how to best implement them in iOS.

(Updated) Chapter 15: “Batten the Hatches with Security Services”—User security and privacy are paramount today, and you find out how to protect your application and user data from attackers with the keychain, certificates, and encryption.

Chapter 16: “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 learn how to adapt your apps to the hardware and get the most out of every platform.

Chapter 17: “Internationalization and Localization”—Although you may want to focus on a single market today, you can do small things to ease the transition to a global market tomorrow. Save money and headaches later, without interrupting today’s development.

Chapter 18: “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 discover the best ways to make this important feature a reality in your application.

(New) Chapter 19: “Debugging”—If only every application were perfect the first time. Luckily, Xcode and LLDB provide many tools to help you track down even the trickiest of bugs. You go beyond the basics and find out how to deal with errors in development and in the field.

(New) Chapter 20: “Performance Tuning Until It Flies”—Performance separates the “okay” app from the exceptional app. It’s critical to optimizing CPU and memory performance, but you also need to optimize battery and network usage. Apple provides an incredible tool for this in Instruments. You discover how to use Instruments to find the bottlenecks, and then how to improve performance once you find the problems.

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 discover the ins and outs of deep iOS.

(New) Chapter 21: “Storyboards and Custom Transitions”—Storyboards can still be confusing and a bit intimidating for developers familiar with nib files. You learn how to use storyboards to your advantage and how to push them beyond the basics.

Chapter 22: “Cocoa’s Biggest Trick: Key-Value Observing”—Many of Apple’s most powerful frameworks rely on KVO for their performance and flexibility. You find out how to leverage the flexibility and speed of KVO, as well as the trick that makes it so transparent.

Chapter 23: “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 24: “Going Offline”—Network programming is difficult, but even harder is providing a seamless offline experience. Discover how to best cache your data and integrate it into your network engine.

(New) Chapter 25: “Data in the Cloud”—Developers are still adapting the new iCloud service. Supporting it quickly can move your app ahead of the pack. You learn your way around this powerful system.

(Updated) Chapter 26: “Fancy Text Layout”—From UIKit to Core Text, iOS is full of ways to display text. iOS adds rich text support throughout UIKit. You find out how to use attributed strings, web views, and Core Text to handle the toughest layout problems.

Chapter 27: “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 discover how to work Core Foundation data types so you can leverage everything iOS has to offer.

Chapter 28: “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 find out 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 27, “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.5 on Mac OS X 10.8 and iOS 6. 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 developer.apple.com/programs/ios to sign up.

Most of the examples in this book will run in the iOS Simulator that comes with Xcode 4.5. You can use the iOS Simulator without an Apple developer license.

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 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.

To find documents at the Apple developer site, visit developer.apple.com, 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. Be 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 tells you 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 either http://iosptl.com/code or www.wiley.com/go/ptl.ios6programming. For example, you will find the following sample code online in the Chapter 26 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;

}

9781118449974-fgin01.tif

Figure 1 Searching for Coding Guidelines for Cocoa

Some source code snippets shown in the book are not comprehensive and are meant to help you understand the chapter. For those instances, you can refer to the files available on the website for the complete source code.

Errata

We try to get everything right, but sometimes things change, sometimes we mistype, and sometimes we’re just mistaken. See http://iosptl.com/code for the latest updates, as well as blog posts on topics that haven’t made the book yet. Send any mistakes you find to [email protected] and [email protected].

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

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