Preface

We’ve been developing for the Cocoa framework from when the Mac first supported it. Since then, we’ve seen the ecosystem of Cocoa and Objective-C development evolve from a small programmer’s niche to one of the most important and influential development environments in the world. (In fact, as 2012 closes, Objective-C is the third most popular programming language according to the TIOBE index, up from fifth most popular in 2011.)

Over the years, we’ve built a lot of large, complex iOS and OS X software, shipping it to millions upon millions of users along the way. We’ve picked up a deep understanding of the toolset, frameworks, and programming language, an understanding crucial to building the best possible software for iOS and OS X. Apple constantly changes things, as the recent introduction of the svelte iPad mini and the stretched iPhone 5 show, but the knowledge necessary to bend the development tools, frameworks and languages to your will stays fairly constant. This book will give you the knowledge, confidence, and appreciation for iOS and OS X development with Cocoa, Cocoa Touch, and Objective-C.

Audience

We assume that you’re a reasonably capable programmer, but we don’t assume you’ve ever developed for iOS or OS X, or used Objective-C before. We also assume that you’re fairly comfortable navigating OS X as a user, and know how to use an iOS device.

Organization of This Book

In this book, we’ll be talking about Cocoa and Cocoa Touch, the frameworks used on OS X and iOS, respectively. Along the way, we’ll also be covering Objective-C, including its syntax and features. Pretty much every chapter contains practical exercises that you can follow along with. The early chapters cover general topics, such as setting up a development environment and coming to grips with the Objective-C language, while later chapters cover specific features of Cocoa and Cocoa Touch.

Here is a concise breakdown of the material each chapter covers:

Chapter 1, Cocoa Development Tools

This chapter introduces Cocoa and Cocoa touch, the frameworks used on OS X and iOS. Introduces Xcode, the IDE that you’ll be using while coding for these platforms. This chapter also covers the Apple Developer Programs, which are necessary if you want to distribute software on the Mac or iTunes App Stores.

Chapter 2, Object-Oriented Programming with Objective-C

This chapter covers object-oriented programming, the programming paradigm used in Objective-C, as well as how Objective-C implements object-oriented programming. This chapter also covers memory management in Cocoa and Cocoa touch, which is one of the most important things to understand when developing for the Mac and for iOS.

Chapter 3, Foundation

This chapter introduces the Foundation framework, which provides the basic data types (like strings, arrays, and dictionaries). This chapter also discusses the underlying design patterns on which much of Cocoa and Cocoa Touch are based.

Chapter 4, Applications on OS X and iOS

This chapter discusses how applications are assembled and operate on Mac and iOS devices. In this chapter, we’ll talk about the application lifecycle on both platforms, as well as how sandboxing affects application access to data and resources.

Chapter 5, Graphical User Interfaces

This chapter demonstrates how user interfaces are loaded and presented to the user. This chapter introduces one of the most powerful concepts provided by Cocoa: nibs, which are predesigned and preconfigured user interfaces, and which can be directly connected to your code. This chapter also discusses Core Animation, the animation system used on OS X and iOS.

Chapter 6, Blocks and Operation Quotes

This chapter introduces blocks, which are an incredibly flexible and useful addition that Objective-C introduces to the C language. Blocks are functions that can be stored in variables and passed around like values. This makes things like callbacks very simple to implement. This chapter also introduces operation queues, which are a straightforward way to work with concurrency without having to deal with threads.

Chapter 7, Drawing Graphics in Views

In this chapter, you’ll learn about the drawing system used on both OS X and iOS, as well as how to draw custom graphics. The retina display is also covered, as well as how view geometry works.

Chapter 8, Audio and Video

This chapter covers audio and video playback using AVFoundation, the audio and video engine. You’ll also learn how to access the iOS photo library and get access to the user’s photos.

Chapter 9, Model Objects and Data Storage

This chapter covers a range of data storage options available on OS X and iOS. Key-Value Coding and Key-Value Observing, preferences, notifications, and filesystem access are all covered. In addition, you’ll learn how to make security-scoped bookmarks, which allow sandboxed apps to retain access to locations that the user has granted your apps permission to use.

Chapter 10, Cocoa Bindings

This chapter covers Cocoa Bindings, a tremendously powerful system that allows you to connect your application’s user interface to an application’s data, without the need for intermediary “glue code.”

Chapter 11, Table Views and Collection Views

This chapter covers table views (an effective way to display multiple rows of data to your user) and collection views, which allow you to display a collection of items to the user.

Chapter 12, Document-Based Applications

This chapter discusses the document systems on both iOS and OS X, which are instrumental in creating applications that work with multiple documents. Here, we discuss the differences in how the two platforms handle documents.

Chapter 13, Networking

Cocoa and Cocoa Touch provide very straightforward tools for accessing networked resources, and this chapter demonstrates how to retrieve information from the internet while keeping the application responsive. This chapter also covers the network service discovery system, Bonjour.

Chapter 14, Working with the Real World

This chapter covers a variety of technologies used to work with the physical world: Core Location, for getting access to the GPS; Core Motion, for learning about how the hardware is moving and oriented, and the printing systems on both iOS and OS X.

Chapter 15, Event Kit

This chapter discusses the calendaring system used on iOS and OS X, and demonstrates how to get access to the user’s calendar. We also discuss considerations for user privacy.

Chapter 16, Instruments and the Debugger

This chapter covers Instruments, the profiler and analysis tool for Mac and iOS applications. An example of a crashing application is discussed, and the cause of the crash is diagnosed and fixed using the application. Additionally, this chapter covers Xcode’s built-in debugger.

Chapter 17, Sharing and Notifications

This chapter discusses how applications can share text, images, and other content with various other services like Twitter and Facebook, using the built-in sharing systems (which don’t require your application to deal with authenticating to these services). Additionally, we’ll cover both push notifications and local notifications, which allow your application to display information to the user without running.

Chapter 18, Nonstandard Apps

Not every program you write will be an app that sits on the user’s home screen, and this chapter tells you how to write three different kinds of non-traditional apps: command-line tools, menu bar apps, and preference panes.

Chapter 19, Working with Text

This chapter covers the string localization system available on iOS and OS X. Here, we discuss data extraction from text using the built-in Data Detectors.

Chapter 20, iCloud

This chapter discusses iCloud, the cloud data storage and syncing system provided by Apple. The functionality and requirements of iCloud are discussed, as well as demonstration apps for both OS X and iOS.

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.

Tip

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

Caution

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: “Learning Cocoa with Objective-C by Jonathon Manning and Paris Buttfield-Addison (O’Reilly). Copyright 2013 Jonathon Manning and Paris Buttfield-Addison, 978-1-449-31849-9.”

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

Safari Books Online (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the world’s leading authors in technology and business.

Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.

Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.

How to Contact Us

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://oreil.ly/Learning_Cocoa.

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

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia

Acknowledgments

Jon thanks his mother, father, and the rest of his crazily extended family for their tremendous support.

Paris thanks his long-suffering mother, whose credit card bankrolled literally hundreds of mobile devices through his childhood; an addiction which, in all likelihood, created the iPhone-, iPad-, mobile-obsessed monster he is today.

We’d both like to thank our editors, Brian Jepson and Rachel Roumeliotis—their skill and advice were invaluable to completing the book. Likewise, all the O’Reilly Media staff we’ve interacted with over the course of writing the book have been the absolute gurus of their fields.

A huge thank-you to Tony Gray and the AUC for the monumental boost they gave us and others listed on this page. We wouldn’t be writing this book if it wasn’t for them.

Thanks also to Neal Goldstein, who deserves full credit and/or blame for getting both of us into the whole book-writing racket.

We’d like to thank the support of the goons at Maclab, who know who they are and continue to stand watch for Admiral Dolphin’s inevitable apotheosis, as well as Professor Christopher Lueg and the rest of the staff at the University of Tasmania for putting up with us.

Additional thanks to Tim N, Nic W, Andrew B, Jess L, and Ash J, for a wide variety of reasons. Finally, very special thanks to Steve Jobs, without whom this book (and many others like it) would not have reason to exist.

Get Learning Cocoa with Objective-C, 3rd 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.