iOS Recipes
Tips and Tricks for Awesome iPhone and iPad Apps
Matt Drance, Paul Warren
Version: B3.0 (April 7, 2011)
Copyright 2011, The Pragmatic Bookshelf. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustdon't allow othersto use your copy of the book. Thanks.
Dave & Andy.
Table of Contents
Copyright 2011, The Pragmatic Bookshelf.
Chapter 1
Change History
The book youre reading is in beta. This means that we update it frequently. This chapter lists the major changes that have been made at each beta release of the book, with the most recent change first.
Beta 3--April 7, 2011
Weve added a few recipes to this Beta: Create a Custom Number Control; basic data models; inspecting complex view hierarchies; and a brand-new version of the splash screen recipe which supports multiple orientations on iPad. A number of recipes have also been updated to address errata and suggestions. Again, be sure to compare any of your projects using these recipes with the newest code from this beta, especially the splash screen.
Errata fixed: 46588, 46612, 46669, 46673, 46676, 46704, 46712, 46715, 46716.
Projects updated:
BasicDataModel
BasicSplashScreen
DebugOutput
NumberSpinControl
PrintSubviews
ScrollViewPins
SmartWebView
CoreText
ScrollingCredits
Beta 2--March 24, 2011
Weve added two new recipes to this Beta on creating "two-tone" table views and adding persistent border shadows to your table content. Weve also addressed a lot of Errata and Forums suggestions. If youve already started using the code in your own projects, be sure to diff these new sources against yours. Thanks for all of the feedback so far!
Errata fixed: 46560, 46562, 46564, 46566, 46569, 46581, 46582, 46583.
Projects updated:
ShadowedTables
TwoToneTables
SmartUserDefaults
SmarterTableCellsNib
PRPAlertView
SmartWebView
MultipartHTTPPost
Copyright 2011, The Pragmatic Bookshelf.
Chapter 2
Introduction
Your goal as programmers is to solve problems. Sometimes the problems are hard, sometimes theyre easy, and sometimes theyre even fun. Maybe theyre not even problems in the colloquial sense of the word, but you are there to discover solutions.
Our goal as authors is to help you solve your problems better and more quickly than before--preferably in that order. We decided to write a recipe-style book that focuses on a specific set of tasks and problems that we attack explicitly, rather than discuss programming issues at a high level.
Thats not to say were not about educating in this book. The blessing of a recipe book is that it gives you trustworthy solutions to problems that you dont feel like discovering on your own. The curse of a recipe book is that you might be tempted to copy and paste the solutions into your project without taking the time to understand them. Its always great to save time by writing less code, but its just as great to think and learn about how you saved that time, and how you can save more of it moving forward.
This book is for anyone familiar with the iOS SDK who is looking to improve the quality and efficiency of their apps. We dont teach you how to write apps in this book, but our hope is that we help you make them better. If youre more of an advanced developer, you may find that you save yourself time and trouble by adopting some of the more sophisticated techniques laid out in the pages that follow.
We wrote many of these recipes with maximum reusability in mind. We werent after demonstrating a technique or a snippet of code that simply gets the job done. Instead, we set out to build solutions that are ready for you to integrate into whatever iPad and iPhone projects youre working on. Some might find their way into your projects with zero changes, but you should feel free to use this recipe book as you would a traditional cookbook. After all, when cooking food from a recipe, you might add or remove ingredients based on what you like, or need, in a meal. When it comes to your own apps and projects, this book is no different: you are invited to extend and edit the projects that accompany these recipes to fit your specific needs.
We developed the recipes in this book to help you get from start to finish, of course, but we hope they also encourage you to think about when and why to choose a certain path. There are often multiple options, especially in an environment like Cocoa. With multiple options, of course, come multiple opinions. In the interest of consistency, we made some decisions early on about certain patterns and approaches to use in this book. Some of these techniques may be familiar to you, some may be employed in a way you hadnt considered, and some may be brand new to you. Regardless, wed like to explain some of those decisions up front so that there are no surprises.
Formatting and Syntax
A number of code snippets in this book are specially formatted to fit the page. A verbose language like Objective-C doesnt always play nicely with character limits, so some of the code may sometimes look unusual. You may encounter terse method or variable names, a seemingly excessive number of temporary variables, and odd carriage returns. Weve tried to preserve the spirit of Cocoa convention as much as possible, but in a few places the printed page won. Dont be alarmed if the coding style suddenly changes from time to time.
Categories
A fair number of recipes make use of categories on standard Apple classes to accomplish tasks. Categories are an incredibly powerful feature of the Objective-C programming language, and they tend to alienate new Cocoa programmers. Categories can quickly pollute namespaces and create (or mask) unexpected behavior in complex class hierarchies. They arent to be feared, but they are to be respected. When considering a category:
Ask yourself if a subclass, or a brand-new class, would be more appropriate. As The Objective-C Programming Language from Apple states: A category is not a substitute for a subclass.
Always prefix category methods when extending a class you dont control (for example, UIApplication), to avoid symbol collisions with future APIs.
Never override defined methods such as -drawRect:
in a category. Doing so breaks the inheritance tree by masking the source class implementation.
Synthesized Instance Variables
Youll find few, if any, instance variable (ivar) declarations in the header files and examples that accompany this book. Weve chosen to exclusively use Objective-C 2.0 properties, with the modern runtimes ivar synthesis feature, for declaring class storage. The result is less typing and less reading, so we can concentrate on the recipe itself.
Private Class Extensions
Private class extensions are another relatively new feature of Objective-C, and theyre frequently used in this book. Private extensions increase readability by minimizing header noise, and they also paint a much clearer picture for adopters or maintainers of your code. The Leveraging Modern Objective-C Class Design recipe introduces both private class extensions and synthesized instance variables for those unfamiliar with either technique.