iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK (2014)
Welcome to iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK!
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 13 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, language features, and iCloud.
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 will have something for you.
The chapters have all been updated to work with iOS 7 Beta 4. As such, there were several iOS 7 features that were still in active development that might not work the same as illustrated in the book after the final version of iOS 7 is released. 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 email@example.com. 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 of 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 8, “Getting Started with iCloud”: This chapter explains how to get started using iCloud, for syncing key-value stores and documents between devices. It walks though setting up an app for iCloud, how to implement the key-value store and document approaches, and how to recognize and resolve conflicts.
Chapter 9, “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 10, “Bluetooth Networking with Game Kit”: This chapter will walk you through creating a real-time Bluetooth-based chat client, enabling you to connect with a friend within Bluetooth range and send text messages back and forth. You will learn how to interact with the Bluetooth functionality of Game Kit, from finding peers to connecting and transferring data.
Chapter 11, “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 12, “Core Data Primer”: Core Data can be a vast and overwhelming topic. This chapter tries to put Core Data in context for the uninitiated, and explains when Core Data might be a good solution for an app and when it might be overkill. It also explains some of the basic concepts of Core Data in simple terminology.
Chapter 13, “Getting Up and Running with Core Data”: This chapter demon-strates 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 14, “Language Features”: Objective-C has been evolving since iOS was introduced. This chapter covers some of the language and compiler-level changes that have occurred, and explains how and why a developer would want to use them. It covers the new literal syntaxes for things like numbers, array, and dictionaries; it also covers blocks, ARC, property declarations, and some oldies but goodies including dot notation, fast enumeration, and method swizzling.
Chapter 15, “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 will walk 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 16, “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 17, “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 18, “Using Keychain to Secure 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 19, “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 20, “Collection Views”: Collection views, a powerful new API introduced in iOS6, 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 21, “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 22, “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 23, “Accessing Photo Libraries”: 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 rebuilding the iOS 6 version of Photos.app.
Chapter 24, “Passbook and PassKit”: With iOS6, Apple introduced Passbook, a standalone app that can store “passes,” or things like 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 25, “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 will introduce 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 25, “Debugging and Instruments,” Chapter 12, “Core Data Primer,” and Chapter 14, “Language Features,” 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 not built using Automatic Reference Counting (ARC); this is by design as well. It is easier to mentally remove the memory management than to add it. The downloadable sample code is made available to suit both tastes; copies of ARC and non-ARC sample code are bundled together. The sample code is prefixed with “ICF” and most, but not all, sample projects are named after the chapter title.
When 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. Additionally, it 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.