Preface

I started developing iPhone applications in late 2007. Between then and now, I have worked on various iPhone OS applications for different companies across the globe. As you might have already guessed, iOS is my favorite platform and Objective-C is my favorite programming language. I find that Objective-C helps programmers write clean code and iOS helps developers write user-friendly and useful applications.

I have coded in other programming languages such as Assembly (using NASM and TASM) and Delphi/Pascal for many years, and I still find myself going through disassembled Objective-C code to find out which method of doing a certain thing or accomplishing a certain result in Objective-C is better optimized on a certain device and/or operating system.

After becoming comfortable with the iOS SDK, I gradually built up a thirst to write a book about the iOS SDK, and with the help of wonderful people at O’Reilly, you are now reading the result of the several hundred hours that have been put into writing new material for iOS 3 and iOS 4, editing, reviewing, revising, and publishing.

So, please go ahead and start exploring the recipes. I hope you’ll find that they are easy to cook and digest!

Audience

I assume you are comfortable with the iOS development environment and know how to create an app for the iPhone or iPad. This book does not get novice programmers started, but presents useful ways to get things done for iOS programmers ranging from novices to experts.

Organization of This Book

In this book, we will discuss frameworks and classes that are available in iOS 3 and iOS 4. In some recipes, you will find code that runs only on iOS 4 and later; in those recipes, I note that you will need the iOS 4 SDK or later to compile the example code.

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

Chapter 1, Working with Objects

Explains how Objective-C classes are structured and how objects can be instantiated. The chapter talks about properties and delegates as well as memory management in Objective-C. Even if you are competent in Objective-C, I strongly suggest that you go through this chapter, even if you are skimming through it, to understand the basic material that is used in the rest of the chapters.

Chapter 2, Implementing Controllers and Views

Describes various approaches to constructing your iOS application’s user interface by taking advantage of different tools the SDK provides. This chapter also introduces you to features that are only available on the iPad, such as the popover and split view controllers.

Chapter 3, Constructing and Using Table Views

Shows how you can work with table views to create professional-looking iOS applications. Table views are very dynamic in nature, and as a result, programmers sometimes have difficulty understanding how they should work with them. By reading this chapter and having a look at and trying out the example code, you will gain the knowledge that is required to comfortably work with table views.

Chapter 4, Core Location and Maps

Describes how you should use Map Kit and Core Location APIs to develop location-aware iOS applications. First you will learn about maps, and then you will learn how to detect a device’s location and tailor your maps with custom annotations. You will also learn about geocoding and reverse geocoding, as well as some of the methods of the Core Location framework, which are only available in the iOS 4 SDK and later.

Chapter 5, Implementing Gesture Recognizers

Demonstrates how to use gesture recognizers, which enable your users to easily and intuitively manipulate the graphical interface of your iOS applications. In this chapter, you will learn how to use all available gesture recognizers in the iOS SDK, with working examples tested on iOS 3 and iOS 4 on different devices such as the iPhone 3GS, iPhone 4, and iPad.

Chapter 6, Networking and XML

Demonstrates how to download data from a URL and parse XML files. You will learn about synchronous and asynchronous connections and their pros and cons. You will also learn about caching files in memory and on disk to avoid consuming the possibly limited bandwidth of an iOS device on which your application could be running.

Chapter 7, Operations, Threads, and Timers

Provides details regarding operations, threads, and timers. Using the material in this chapter, you can develop modern multithreaded iOS applications. In addition, you will learn about operations and operation queues, and how to avoid implementing your own threads and instead let iOS do it for you.

Chapter 8, Audio and Video

Discusses the AV Foundation and Media Player frameworks that are available on the iOS SDK. You will learn how to play audio and video files and how to handle interruptions, such as a phone call, while the audio or video is being played on both iOS 3 and iOS 4. This chapter also explains how to record audio using an iOS device’s built-in microphone(s). At the end of the chapter, you will learn how to access the iPod Library and play its media content, all from inside your application.

Chapter 9, Address Book

Explains the Address Book framework and how to retrieve contacts, groups, and their information from the Address Book database on an iOS device. The Address Book framework is composed entirely of C APIs. Because of this, many Objective-C developers find it difficult to use this framework compared to frameworks that provide an Objective-C interface. After reading this chapter and trying the examples for yourself, you will feel much more confident using the Address Book framework.

Chapter 10, Camera and the Photo Library

Demonstrates how you can determine the availability of front- and back-facing cameras on an iOS device. Some of the recipes in this chapter are specific to iOS 4, with the rest working on both iOS 3 and iOS 4. You will also learn how to access the Photo Library using the Assets Library framework which is available in iOS 4 and later. At the end of the chapter, you will learn about editing videos right on an iOS device using a built-in view controller.

Chapter 11, Multitasking

Explains, with examples, how to create multitasking-aware applications that run beautifully on iOS 4. You will learn about background processing, from playing audio and retrieving users’ locations in the background, to downloading content from a URL while your application is running in the background.

Chapter 12, Core Data

Describes how to maintain persistent storage for your iOS applications using Core Data. You will learn how to add to, delete from, and edit Core Data objects and how to boost access to data in a table view. In addition, you will learn how to manage relationships between Core Data objects.

Chapter 13, Event Kit

Demonstrates the use of the Event Kit and Event Kit UI frameworks, which are available on iOS 4 and later, in order to manage calendars and events on an iOS device. You will see how to create, modify, save, and delete events. You will also learn, through examples, how to add alarms to calendar events and how to set up CalDAV calendars so that you can share a single calendar among multiple devices.

Chapter 14, Graphics

Introduces the Core Graphics framework. You will learn how to draw images and text on a graphics context, grab the contents of a graphics context and save it as an image, and much more.

Chapter 15, Core Motion

Explains the Core Motion framework, which is new in iOS 4. Using Core Motion, you will access the accelerometer and the gyroscope on an iOS device. You will also learn how to detect shakes on a device. Of course, not all iOS devices are equipped with an accelerometer and a gyroscope, so you will also learn how to detect the availability of the required hardware.

Additional Resources

From time to time, I refer to official Apple documentation. Some of Apple’s descriptions are right on the mark, and there is no point in trying to restate them. Throughout this book, I have listed the most important documents and guides in the official Apple documentation that every professional iOS developer should read.

For starters, I suggest that you have a look at the “iPhone Human Interface Guidelines” and the “iPad Human Interface Guidelines.” These two documents will tell you everything you should know about developing engaging and intuitive user interfaces for the iPhone/iPod and the iPad. Every iOS programmer must read these documents. In fact, I believe these documents must be read by the product design and development teams of any company that develops iOS applications.

I also suggest that you skim through the “iOS Application Programming Guide” in the iOS Reference Library for some tips and advice on how to make great iOS applications:

https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html

One of the things you will notice when reading Chapter 11 is the use of block objects. This book concisely explains block objects, but if you require further details on the subject, I suggest you read “A Short Practical Guide to Blocks,” available at this URL:

https://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/index.html%23/apple_ref/doc/uid/TP40009758

In Chapter 7, you will learn about operations. To be able to implement custom operations, as you will see later, you must have a basic knowledge of key-value coding (KVC). If you require more information about KVC, I recommend that you read the “Key-Value Coding Programming Guide,” available at the following URL:

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html

Throughout this book, you will see references to “bundles” and loading images and data from bundles. You will read a concise overview about bundles in this book, but if you require further information, head over to the “Bundle Programming Guide,” available at this URL:

https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html

Conventions Used in This Book

The following typographical conventions are used in this book:

Italic

Indicates new terms, URLs, filenames, file extensions, and directories

Constant width

Indicates variables and other code elements, the contents of files, and the output from commands

Constant width bold

Highlights text in examples that is new or particularly significant in a recipe

Constant width italic

Shows text that should be replaced with user-supplied values

Note

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

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: “iOS 4 Programming Cookbook, by Vandad Nahavandipoor (O’Reilly). Copyright 2011 Vandad Nahavandipoor, 978-1-449-38822-5.”

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

We’d Like to Hear from You

Every example and code snippet in this book has been tested on the iPhone 4, iPad, iPhone 3GS, and iPhone/iPad Simulator, but occasionally you may encounter problems—for example, if you have a different version of the SDK than the version on which the example code was compiled and tested. The information in this book has also been verified at each step of the production process. However, mistakes and oversights can occur, and we will gratefully receive details of any you find, as well as any suggestions you would like to make for future editions. You can contact the author and editors at:

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://oreilly.com/catalog/9781449388225

There is also a companion website to this book where you can see all the examples with color-highlighted syntax:

http://www.ios4cookbook.com

To comment or ask technical questions about this book, send email to the following address, mentioning the book’s ISBN number (9781449388225):

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

http://www.oreilly.com

Safari® Books Online

Note

Safari Books Online is an on-demand digital library that lets you easily search more than 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

Acknowledgments

I have always loved writing and running my programs on computers. I look at programming as a way to speak to whatever computer the program runs on. To me, programming is a way to actually connect with the computer and give it instructions and listen to what it says in return.

I have been exceptionally lucky to have almost always found the right people to help me find the right path in whatever journey I’ve started in my life. First and foremost, I would like to thank my beloved fiancée, Agnieszka Marta Dybowska, for her unconditional love and support throughout the years and for the many hours she had to spend without me while I was working on this book. Your strong enthusiasm for writing and journalism has greatly moved me, and I sincerely hope that one day you will gather enough material to be able to write your book.

I also want to thank Brian Jepson (whose many hidden talents are yet to be discovered!) for giving me a chance to work on this book. This book would have been impossible if it wasn’t for Brian’s consistent urge to improve the outline and the table of contents that I originally sent him. This reminds me to thank Andy Oram, whom I would like to call the virtual second writer of this book. Andy’s perfectionism and his undeniable desire to finely form every single sentence you read in this book are absolutely impressive. I must also thank Sarah Kim and Rachel James for helping me update my profile page on O’Reilly’s website. I also appreciate Meghan Blanchette’s help in doing the initial paperwork for this book.

I want to say a big thank you to my technical reviewers, Eric Blair and Alasdair Allan, for all their helpful insight. Kirk Pattinson, Gary McCarville, and Sushil Shirke are among the people who have greatly influenced me to become who I am today. Thank you to Sushil for being a great mentor and colleague and for providing continuous support. Thanks to Kirk for believing that I was up to the challenge of working on some high-profile iOS applications. Thank you to Gary for his support while I worked on this project, and for being a wonderful mentor.

Last but not least, I would like to sincerely thank Apple and its employees for making such a wonderful operating system and SDK. It’s truly a great pleasure to work with the iOS SDK, and I hope you, the reader, will enjoy working with it as much as I do.

Get iOS 4 Programming Cookbook 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.