Welcome to Mastering iOS Frameworks: Beyond the Basics!
There are hundreds of “getting started with iOS” books available to choose from, and there are dozens of advanced books in specific topics, such as Core Data or Security. There was, however, a disturbing lack of books that would bridge the gap between beginner and advanced niche topics.
This publication aims to provide development information on the intermediate-to-advanced topics that are otherwise not worthy of standalone books. It’s not that the topics are uninteresting or lackluster; it’s that they are not large enough topics. From topics such as working with JSON to accessing photo libraries, these are frameworks that professional iOS developers use every day but are not typically covered elsewhere.
Additionally, several advanced topics are covered to the level that many developers need in order to just get started. Picking up a 500-page Core Data book is intimidating, whereas Chapter 15 of this book provides a very quick and easy way to get started with Core Data. Additional introductory chapters are provided for debugging and instruments, TextKit, HomeKit, HealthKit, and CloudKit.
Topics such as Game Center leaderboards and achievements, AirPrint, music libraries, Address Book, and Passbook are covered in their entirety. Whether you just finished your first iOS project or you are an experienced developer, this book has something for you.
The chapters have all been updated to work with iOS 8. Please let us know if you encounter issues and we will release updates and corrections.
If you have suggestions, bug fixes, corrections, or anything else you’d like to contribute to a future edition, please contact us at firstname.lastname@example.org. We are always interested in hearing what would make this book better and are very excited to continue refining it.
—Kyle Richter and Joe Keeley
Every effort has been made to keep the examples and explanations simple and easy to digest; however, this is to be considered an intermediate to advanced book. To be successful with it, you should have a basic understanding of iOS development, Objective-C, and C. Familiarity with the tools such as Xcode, Developer Portal, iTunes Connect, and Instruments is also assumed. Refer to Programming in Objective-C, by Stephen G. Kochan, and Learning iOS Development, by Maurice Sharp, Rod Strougo, and Erica Sadun, for basic Objective-C and iOS skills.
What You’ll Need
Although you can develop iOS apps in the iOS simulator, it is recommended that you have at least one iOS device available for testing:
- Apple iOS Developer Account: The latest version of the iOS developer tools including Xcode and the iOS SDKs can be downloaded from Apple’s Developer Portal (http://developer.apple.com/ios). To ship an app to the App Store or to install and test on a personal device, you will also need a paid developer account at $99 per year.
- Macintosh Computer: To develop for iOS and run Xcode, you will need a modern Mac computer capable of running the latest release of OS X.
- Internet Connection: Many features of iOS development require a constant Internet connection for your Mac as well as for the device you are building against.
How This Book Is Organized
With few exceptions (Game Center and Core Data), each chapter stands on its own. The book can be read cover to cover but any topic can be skipped to when you find a need for that technology; we wrote it with the goal of being a quick reference for many common iOS development tasks.
Here is a brief overview of the chapters you will encounter:
- Chapter 1, “UIKit Dynamics”: iOS 7 introduced UI Kit Dynamics to add physics-like animation and behaviors to UIViews. You will learn how to add dynamic animations, physical properties, and behaviors to standard objects. Seven types of behaviors are demonstrated in increasing difficulty from gravity to item properties.
- Chapter 2, “Core Location, MapKit, and Geofencing”: iOS 6 introduced new, Apple-provided maps and map data. This chapter covers how to interact with Core Location to determine the device’s location, how to display maps in an app, and how to customize the map display with annotations, overlays, and callouts. It also covers how to set up regional monitoring (or geofencing) to notify the app when the device has entered or exited a region.
- Chapter 3, “Leaderboards”: Game Center leaderboards provide an easy way to add social aspects to your iOS game or app. This chapter introduces a fully featured iPad game called Whack-a-Cac, which walks the reader through adding leaderboard support. Users will learn all the required steps necessary for implementing Game Center leaderboards, as well as get a head start on implementing leaderboards with a custom interface.
- Chapter 4, “Achievements”: This chapter continues on the Whack-a-Cac game introduced in Chapter 3. You will learn how to implement Game Center achievements in a fully featured iPad game. From working with iTunes Connect to displaying achievement progress, this chapter provides all the information you need to quickly get up and running with achievements.
- Chapter 5, “Getting Started with Address Book”: Integrating a user’s contact information is a critical step for many modern projects. Address Book framework is one of the oldest available on iOS; in this chapter you’ll learn how to interact with that framework. You will learn how to use the people picker, how to access the raw address book data, and how to modify and save that data.
- Chapter 6, “Working with Music Libraries”: This chapter covers how to access the user’s music collection from a custom app, including how to see informational data about the music in the collection, and how to select and play music from the collection.
- Chapter 7, “Implementing HealthKit”: HealthKit provides a centralized location for health information that can be shared among apps. This chapter explains how to get started with HealthKit, how to access information available in HealthKit, and how to read and write various types of health data.
- Chapter 8, “Implementing HomeKit”: This chapter explains how to get started using HomeKit, which enables iOS devices to communicate with home automation technology. It explains how to set up a home in HomeKit, and how to discover, set up, and interact with home automation devices such as lights, locks, and garage door openers.
- Chapter 10, “Notifications”: Two types of notifications are supported by iOS: local notifications, which function on the device with no network required, and remote notifications, which require a server to send a push notification through Apple’s Push Notification Service to the device over the network. This chapter explains the differences between the two types of notifications, and demonstrates how to set them up and get notifications working in an app.
- Chapter 11, “Cloud Persistence with CloudKit”: CloudKit offers public and private remote data storage, with notifications for changes in data. This chapter explains the basic CloudKit concepts, and illustrates how to build an app that uses CloudKit for storing and syncing both private and public data remotely.
- Chapter 12, “Extensions”: Extensions provide a way to access an app’s functionality outside the app’s sandbox. This chapter explains the different types of extensions that are available, and illustrates how to create a Today extension and an Apple Watch extension.
- Chapter 13, “Handoff”: Handoff is one of the Continuity features introduced with iOS 8 and Yosemite. It enables the user to switch between devices and have an activity seamlessly move from one device to another. This chapter explains the basic Handoff mechanisms, and how to implement Handoff for developer-defined activities and document-based activities.
- Chapter 14, “AirPrint”: An often-underappreciated feature of the iOS, AirPrint enables the user to print documents and media to any wireless-enabled AirPrint-compatible printer. Learn how to quickly and effortlessly add AirPrint support to your apps. By the end of this chapter you will be fully equipped to enable users to print views, images, PDFs, and even rendered HTML.
- Chapter 15, “Getting Up and Running with Core Data”: This chapter demonstrates how to set up an app to use Core Data, how to set up a Core Data data model, and how to implement many of the most commonly used Core Data tools in an app. If you want to start using Core Data without digging through a 500-page book, this chapter is for you.
- Chapter 16, “Integrating Twitter and Facebook Using Social Framework”: Social integration is the future of computing, and it is accepted that all apps have social features built in. This chapter walks you through adding support for Facebook and Twitter to your app using the Social Framework. You will learn how to use the built-in composer to create new Twitter and Facebook posts. You will also learn how to pull down feed information from both services and how to parse and interact with that data. Finally, using the frameworks to send messages from custom user interfaces is covered. By the end of this chapter, you will have a strong background in Social Framework as well as working with Twitter and Facebook to add social aspects to your apps.
- Chapter 17, “Working with Background Tasks”: Being able to perform tasks when the app is not the foreground app was a big new feature introduced in iOS 4, and more capabilities have been added since. This chapter explains how to perform tasks in the background after an app has moved from the foreground, and how to perform specific background activities allowed by iOS.
- Chapter 18, “Grand Central Dispatch for Performance”: Performing resource-intensive activities on the main thread can make an app’s performance suffer with stutters and lags. This chapter explains several techniques provided by Grand Central Dispatch for doing the heavy lifting concurrently without affecting the performance of the main thread.
- Chapter 19, “Using Keychain and TouchID to Secure and Access Data”: Securing user data is important and an often-overlooked stage of app development. Even large public companies have been called out in the news over the past few years for storing user credit card info and passwords in plain text. This chapter provides an introduction to not only using the Keychain to secure user data but developmental security as a whole. By the end of the chapter, you will be able to use Keychain to secure any type of small data on users’ devices and provide them with peace of mind.
- Chapter 20, “Working with Images and Filters”: This chapter covers some basic image-handling techniques, and then dives into some advanced Core Image techniques to apply filters to images. The sample app provides a way to explore all the options that Core Image provides and build filter chains interactively in real time.
- Chapter 21, “Collection Views”: Collection views, a powerful new API introduced in iOS 6, give the developer flexible tools for laying out scrollable, cell-based content. In addition to new content layout options, collection views provide exciting new animation capabilities, both for animating content in and out of a collection view and for switching between collection view layouts. The sample app demonstrates setting up a basic collection view, a customized flow layout collection view, and a highly custom, nonlinear collection view layout.
- Chapter 22, “Introduction to TextKit”: iOS 7 introduced TextKit as an easier-to-use and greatly expanded update to Core Text. TextKit enables developers to provide rich and interactive text formatting to their apps. Although TextKit is a very large subject, this chapter provides the basic groundwork to accomplish several common tasks, from adding text wrapping around an image to inline custom font attributes. By the end of this chapter, you will have a strong background in TextKit and have the groundwork laid to explore it more in depth.
- Chapter 23, “Gesture Recognizers”: This chapter explains how to make use of gesture recognizers in an app. Rather than dealing with and interpreting touch data directly, gesture recognizers provide a simple and clean way to recognize common gestures and respond to them. In addition, custom gestures can be defined and recognized using gesture recognizers.
- Chapter 24, “Accessing the Photo Library”: The iPhone has actually become a very popular camera, as evidenced by the number of photos that people upload to sites such as Flickr. This chapter explains how to access the user’s photo library, and handle photos and videos in a custom app. The sample app demonstrates building some of the concepts from the iOS 8 version of Photos.app.
- Chapter 25, “Passbook and PassKit”: With iOS 6, Apple introduced Passbook, a standalone app that can store “passes,” or such things as plane tickets, coupons, loyalty cards, or concert tickets. This chapter explains how to set up passes, how to create and distribute them, and how to interact with them in an app.
- Chapter 26, “Debugging and Instruments”: One of the most important aspects of development is to be able to debug and profile your software. Rarely is this topic covered even in a cursory fashion. This chapter introduces you to debugging in Xcode and performance analysis using Instruments. Starting with a brief history of computer bugs, the chapter walks you through common debugging tips and tricks. Topics of breakpoints and debugger commands are briefly covered, and the chapter concludes with a look into profiling apps using the Time Profiler and memory analysis using Leaks. By the end of this chapter, you will have a clear foundation on how to troubleshoot and debug iOS apps on both the simulator and the device.
About the Sample Code
Each chapter of this book is designed to stand by itself; therefore, each chapter with the exception of Chapter 26, “Debugging and Instruments,” has its own sample project. Chapter 3, “Leaderboards,” and Chapter 4, “Achievements,” share a base sample project, but each expands on that base project in unique ways. Each chapter provides a brief introduction to the sample project and walks the reader through any complex sections of the sample project not relating directly to the material in the chapter.
Every effort has been made to create simple-to-understand sample code, which often results in code that is otherwise not well optimized or not specifically the best way of approaching a problem. In these circumstances the chapter denotes where things are being done inappropriately for a real-world app. The sample projects are not designed to be standalone or finished apps; they are designed to demonstrate the functionality being discussed in the chapter. The sample projects are generic with intention; the reader should be able to focus on the material in the chapter and not the unrelated sample code materials. A considerable amount of work has been put into removing unnecessary components from the sample code and condensing subjects into as few lines as possible.
Many readers will be surprised to see that the sample code in the projects is built with Objective-C instead of Swift; this is by design as well. Since all the APIs illustrated are built with Objective-C, it is easier to interact with them using Objective-C, rather than add an additional layer of complexity by using Swift. The concepts illustrated are easily portable to Swift after the reader is comfortable with developing in Swift. The sample code is prefixed with “ICF” and most, but not all, sample projects are named after the chapter title.
When you’re working with the Game Center chapters, the bundle ID is linked to a real app, which is in our personal Apple account; this ensures that examples continue to work. It also has the small additional benefit of populating multiple users’ data as developers interact with the sample project. For chapters dealing with iCloud, Push Notifications, and Passbook, the setup required for the apps is thoroughly described in the chapter, and must be completed using a new App ID in the reader’s developer account in order to work.
Getting the Sample Code
You will be able to find the most up-to-date version of the source code at any moment at https://github.com/dfsw/icf, in the Mastering folder. The code is publicly available and open source. Each chapter is broken down into its own zip file containing an Xcode project; there are no chapters with multiple projects. We encourage readers to provide feedback on the source code and make recommendations so that we can continue to refine and improve it long after this book has gone to print.
Installing Git and Working with GitHub
Git is a version control system that has been growing in popularity for several years. To clone and work with the code on GitHub, you will want to first install Git on your Mac. A command-line version Git is included in the Xcode command-line tool installation, or a current installer for Git can be found at http://git-scm.com/downloads. Additionally, there are several GUI front ends for Git, even one written by GitHub, which might be more appealing to developers who avoid command-line interfaces. If you do not want to install Git, GitHub also allows for downloading the source files as a zip.
GitHub enables users to sign up for a free account at https://github.com/signup/free. After Git has been installed, from the terminal’s command line $git clone email@example.com:dfsw/icf.git will download a copy of the source code into the current working directory. The sample code for this version of the book is in the Mastering folder. You are welcome to fork and open pull requests with the sample code projects.
Contacting the Authors
If you have any comments or questions about this book, please drop us an e-mail message at firstname.lastname@example.org, or on Twitter at @kylerichter and @jwkeeley.
This book could not have existed without a great deal of effort from far too many behind-the-scenes people; although there are only two authors on the cover, dozens of people were responsible for bringing this book to completion. We would like to thank Trina MacDonald first and foremost; without her leadership and her driving us to meet deadlines, we would never have been able to finish. The editors at Pearson have been exceptionally helpful; their continual efforts show on every page, from catching our typos to pointing out technical concerns. The dedicated work of Niklas Saers, Olivia Basegio, Justin Williams, Sheri Replin, Elaine Wiley, Cheri Clark, Chuti Prasertsith, and Gloria Shurick made the following pages possible.
We would also like to thank Jordan Langille of Langille Design (http://jordanlangille.com) for providing the designs for the Whack-a-Cac game featured in Chapters 3 and 4. His efforts have made the Game Center sample projects much more compelling.
The considerable amount of time spent working on this book was shouldered not only by us but also by our families and co-workers. We would like to thank everyone who surrounds us in our daily lives for taking a considerable amount of work off of our plates, as well as understanding the demands that a project like this brings.
Finally, we would like to thank the community at large. All too often we consulted developer forums, blog posts, and associates to ask questions or provide feedback. Without the hard efforts of everyone involved in the iOS community, this book would not be nearly as complete.
About the Authors
Kyle Richter is the Chief Executive Officer at MartianCraft, an award-winning Mobile Development Studio. Kyle began developing software in the early 1990s and has always been dedicated to the Apple ecosystem. He has authored and coauthored several books on iOS development, including Beginning iOS Game Center Development, Beginning Social Game Development, and iOS Components and Frameworks. Between running day-to-day operations at MartianCraft, Kyle travels the world speaking on development and entrepreneurship. He currently calls the Florida Keys home, where he spends his time with his border collie. He can be found on Twitter at @kylerichter.
Joe Keeley is a Partner and Lead Engineer at MartianCraft. Joe provides technical leadership on iOS projects for clients, and has led a number of successful client projects to completion. He has liked writing code since first keying on an Apple II, and has worked on a wide variety of technology and systems projects in his career. Joe has presented several technical topics at iOS and Mac conferences around the U.S. Joe lives in Denver, Colorado, with his wife and two daughters, and hopes to get back into competitive fencing again in his spare time. He can be reached on Twitter at @jwkeeley.