Preface

Welcome to another iOS Cookbook!

With iOS 6, Apple’s mobile device family has reached new levels of excitement and possibility. This Cookbook is here to help you start developing. This revision introduces new features announced at the latest WWDC, showing you how to incorporate them into your applications.

For this edition, my publishing team has sensibly split the Cookbook material into manageable print volumes. This book, The Advanced iOS 6 Developer’s Cookbook, centers on common frameworks such as StoreKit, GameKit, and Core Location and handy techniques such as image manipulation typesetting. It helps you build applications that leverage special-purpose libraries and move beyond the basics. This volume is for those who have a strong grasp on iOS development and are looking for practical how-to’s for specialized areas.

Its companion volume, The Core iOS 6 Developer’s Cookbook, provides solutions for the heart of day-to-day development. It covers all the classes you need for creating iOS applications using standard APIs and interface elements. It contains the recipes you need for working with graphics, touches, and views to create mobile applications.

Finally, there’s Learning iOS 6: A Hands-On Guide to the Fundamentals of iOS Programming, which covers much of the tutorial material that used to compose the first several chapters of the Cookbook. There you can find all the fundamental how-to’s you need to learn iOS 6 development from the ground up. From Objective-C to Xcode, debugging to deployment, Learning iOS 6 teaches you how to start with Apple’s development tool suite.

As in the past, you can find sample code at github. You’ll find the repository for this Cookbook at https://github.com/erica/iOS-6-Cookbook, all of it refreshed for iOS 6 after WWDC 2012.

If you have suggestions, bug fixes, corrections, or anything else you’d like to contribute to a future edition, please contact me at [email protected]. Let me thank you all in advance. I appreciate all feedback that helps make this a better, stronger book.

—Erica Sadun, September 2012

What You Need

It goes without saying that, if you plan to build iOS applications, you need at least one iOS device to test your application, preferably a new model iPhone or tablet. The following list covers the basics of what you need to begin:

Apple’s iOS SDK—You can download the latest version of the iOS SDK from Apple’s iOS Dev Center (http://developer.apple.com/ios). If you plan to sell apps through the App Store, become a paid iOS developer. This costs $99/year for individuals and $299/year for enterprise (that is, corporate) developers. Registered developers receive certificates that enable them to “sign” and download their applications to their iPhone/iPod touch for testing and debugging and to gain early access to prerelease versions of iOS. Free-program developers can test their software on the Mac-based simulator but cannot deploy to devices or submit to the App Store.


University Student Program

Apple also offers a University Program for students and educators. If you are a computer science student taking classes at the university level, check with your professor to see whether your school is part of the University Program. For more information about the iPhone Developer University Program, see http://developer.apple.com/support/iphone/university.


A modern Mac running Mac OS X Lion (v 10.7) or, preferably, Mac OS X Mountain Lion (v 10.8)—You need plenty of disk space for development, and your Mac should have as much RAM as you can afford to put into it.

An iOS device—Although the iOS SDK includes a simulator for you to test your applications in, you really do need to own iOS hardware to develop for the platform. You can tether your unit to the computer and install the software you’ve built. For real-life App Store deployment, it helps to have several units on hand, representing the various hardware and firmware generations, so you can test on the same platforms your target audience uses.

An Internet connection—This connection enables you to test your programs with a live Wi-Fi connection and with an EDGE or 3G service.

Familiarity with Objective-C—To program for the iPhone, you need to know Objective-C 2.0. The language is based on ANSI C with object-oriented extensions, which means you also need to know a bit of C. If you have programmed with Java or C++ and are familiar with C, you can make the move to Objective-C.

Your Roadmap to Mac/iOS Development

One book can’t be everything to everyone. Try as I might, if we were to pack everything you need to know into this book, you wouldn’t be able to pick it up. (As it stands, this book offers an excellent tool for upper-body development. Please don’t sue if you strain yourself lifting it.) There is, indeed, a lot you need to know to develop for the Mac and iOS platforms. If you are just starting out and don’t have any programming experience, your first course of action should be to take a college-level course in the C programming language. Although the alphabet might start with the letter A, the root of most programming languages, and certainly your path as a developer, is C.

When you know C and how to work with a compiler (something you’ll learn in that basic C course), the rest should be easy. From there, you can hop right on to Objective-C and learn how to program with that alongside the Cocoa frameworks. The flowchart shown in Figure P-1 shows you key titles offered by Pearson Education that can help provide the training you need to become a skilled iOS developer.

Image

Figure P-1. A roadmap to becoming an iOS developer

When you know C, you have a few options for learning how to program with Objective-C. If you want an in-depth view of the language, you can either read Apple’s documentation or pick up one of these books on Objective-C:

Objective-C Programming: The Big Nerd Ranch Guide, by Aaron Hillegass (Big Nerd Ranch, 2012)

Learning Objective-C: A Hands-on Guide to Objective-C for Mac and iOS Developers, by Robert Clair (Addison-Wesley, 2011)

Programming in Objective-C 2.0, Fourth Edition, by Stephen Kochan (Addison-Wesley, 2012)

With the language behind you, next up is tackling Cocoa and the developer tools, otherwise known as Xcode. For that, you have a few different options. Again, you can refer to Apple’s documentation on Cocoa and Xcode,2 or if you prefer books, you can learn from the best. Aaron Hillegass, founder of the Big Nerd Ranch in Atlanta,3 is the coauthor of iOS Programming: The Big Nerd Ranch Guide, Second Edition, and author of Cocoa Programming for Mac OS X, soon to be in its fourth edition. Aaron’s book is highly regarded in Mac developer circles and is the most-recommended book you’ll see on the cocoa-dev mailing list. To learn more about Xcode, look no further than Fritz Anderson’s Xcode 4 Unleashed from Sams Publishing.


Note

There are plenty of other books from other publishers on the market, including the bestselling Beginning iPhone 4 Development, by Dave Mark, Jack Nutting, and Jeff LaMarche (Apress, 2011). Another book that’s worth picking up if you’re a total newbie to programming is Beginning Mac Programming, by Tim Isted (Pragmatic Programmers, 2011). Don’t just limit yourself to one book or publisher. Just as you can learn a lot by talking with different developers, you can learn lots of tricks and tips from other books on the market.


To truly master Mac development, you need to look at a variety of sources: books, blogs, mailing lists, Apple’s documentation, and, best of all, conferences. If you get the chance to attend WWDC, you’ll know what I’m talking about. The time you spend at those conferences talking with other developers, and in the case of WWDC, talking with Apple’s engineers, is well worth the expense if you are a serious developer.

How This Book Is Organized

This book offers single-task recipes for the most common issues new iOS developers face: laying out interface elements, responding to users, accessing local data sources, and connecting to the Internet. Each chapter groups together related tasks, enabling you to jump directly to the solution you’re looking for without having to decide which class or framework best matches that problem.

The iOS 6 Developer’s Cookbook offers you “cut-and-paste convenience,” which means you can freely reuse the source code from recipes in this book for your own applications and then tweak the code to suit your app’s needs.

Here’s a rundown of this book’s chapters:

Chapter 1, “Device-Specific Development—Each iOS device represents a meld of unique, shared, momentary, and persistent properties. These properties include the device’s current physical orientation, its model name, its battery state, and its access to onboard hardware. This chapter looks at the device from its build configuration to its active onboard sensors. It provides recipes that return a variety of information items about the unit in use.

Chapter 2, “Documents and Data Sharing—Under iOS, applications can share information and data as well as move control from one application to another using several system-supplied features. This chapter introduces the ways you can integrate documents and data sharing between applications. You see how to add these features into your applications and use them smartly to make your app a cooperative citizen of the iOS ecosystem.

Chapter 3, “Core Text—This chapter introduces attributed text processing and explores how you can build text features into your apps. You read about adding attributed strings to common UIKit elements, how to create Core Text-powered views, and how to break beyond lines for freeform text typesetting. After reading this chapter, you’ll have discovered the power that Core Text brings to iOS.

Chapter 4, “Geometry—Although UIKit requires less applied math than, say, Core Animation or Open GL, geometry plays an important role when working with Bezier paths and view transforms. Why do you need geometry? It helps you manipulate views in nonstandard ways, including laying out text along custom paths and performing path-follow types of animation. If your eyes glaze over at the mention of Bezier curves, Convex Hulls, and splines, this chapter can help demystify these terms, enabling you to add some powerful customization options to your toolbox.

Chapter 5, “Networking—Apple has lavished iOS with a solid grounding in all kinds of network computing and its supporting technologies. The networking chapter in the Core Cookbook introduced network status checks, synchronous and asynchronous downloads, JSON, and XML parsing. This chapter continues that theme by introducing more advanced techniques. These include authentication challenges, using the system keychain, working with OAuth, and so forth. Here are handy approaches that should help with your development.

Chapter 6, “Images—Images are abstract representations, storing data that makes up pictures. This chapter introduces Cocoa Touch images, specifically the UIImage class, and teaches you all the basic know-how you need for working with image data on iOS. In this chapter, you learn how to load, store, and modify image data in your applications. You discover how to process image data to create special effects, how to access images on a byte-by-byte basis, and more.

Chapter 7, “Camera—Cameras kick images up to the next level. They enable you to integrate live feeds and user-directed snapshots into your applications, and provide raw data sourced from the real world. In this chapter, you read about image capture. You discover how to take pictures using Apple-sourced classes and how to roll your own from scratch. You learn about controlling image metadata and how to integrate live feeds with advanced filtering. This chapter focuses on image capture from a hardware point of view. Whether you’re switching on the camera flash LED or detecting faces, this chapter introduces the ins and outs of iOS image capture technology.

Chapter 8, “Audio—The iOS device is a media master; its built-in iPod features expertly handle both audio and video. The iOS SDK exposes that functionality to developers. A rich suite of classes simplifies media handling via playback, search, and recording. This chapter introduces recipes that use those classes for audio, presenting media to your users and letting your users interact with that media. You see how to build audio players and audio recorders. You discover how to browse the iPod library and how to choose what items to play.

Chapter 9, “Connecting to the Address Book—This chapter introduces the Address Book and demonstrates how to use its frameworks in your applications. You read about accessing information on a contact-by-contact basis, how to modify and update contact information, and how to use predicates to find just the contact you’re interested in. This chapter also covers the GUI classes that provide interactive solutions for picking, viewing, and modifying contacts.

Chapter 10, “Location—Where you compute is fast becoming just as important as how you compute and what you compute. iOS is constantly on the go, traveling with its users throughout the course of the day. Core Location infuses iOS with on-demand geopositioning. MapKit adds interactive in-application mapping, enabling users to view and manipulate annotated maps. With Core Location and MapKit, you can develop applications that help users meet up with friends, search for local resources, or provide location-based streams of personal information. This chapter introduces these location-aware frameworks and shows you how you can integrate them into your iOS applications.

Chapter 11, “GameKit—This chapter introduces various ways you can create connected game play through GameKit. GameKit offers features that enable your applications to move beyond a single-player/single-device scenario toward using Game Center and device-to-device networking. Apple’s Game Center adds a centralized service that enables your game to offer shared leaderboards and Internet-based matches. GameKit also provides an ad-hoc networking solution for peer-to-peer connectivity.

Chapter 12, “StoreKit—StoreKit offers in-app purchasing that integrates into your software. With StoreKit, end users can use their iTunes credentials to buy unlockable features, media subscriptions, or consumable assets, such as fish food or sunlight, from within an application. This chapter introduces StoreKit and shows you how to use the StoreKit API to create purchasing options for users.

Chapter 13, “Push Notification—When off-device services need to communicate directly with users, push notifications provide a solution. Just as local notifications enable apps to contact users at scheduled times, push notifications deliver messages from web-based systems. Push notifications enable devices to display an alert, play a custom sound, or update an application badge. In this way, off-device services connect with an iOS-based client, enabling them to know about new data or updates. This chapter introduces all the push notification basics you need to know.

About the Sample Code

For the sake of pedagogy, this book’s sample code uses a single main.m file. This is not how people normally develop iPhone or Cocoa applications, or, honestly, how they should be developing them, but it provides a great way of presenting a single big idea. It’s hard to tell a story when readers must look through five or seven or nine individual files at once. Offering a single file concentrates that story, allowing access to that idea in a single chunk.

These examples are not intended as stand-alone applications. They are there to demonstrate a single recipe and a single idea. One main.m file with a central presentation reveals the implementation story in one place. You can study these concentrated ideas and transfer them into normal application structures, using the standard file structure and layout. The presentation in this book does not produce code in a standard day-to-day best-practices approach. Instead, it offers concise solutions that you can incorporate back into your work as needed.

Contrast that to Apple’s standard sample code, where you must comb through many files to build up a mental model of the concepts that are being demonstrated. Those examples are built as full applications, often doing tasks that are related to but not essential to what you need to solve. Finding just those relevant portions is a lot of work. The effort may outweigh any gains.

In this book, you find exceptions to this one-file-with-the-story rule: The Cookbook provides standard class and header files when a class implementation is the recipe. Instead of highlighting a technique, some recipes offer these classes and categories (that is, extensions to a preexisting class rather than a new class). For those recipes, look for separate .m and .h files in addition to the skeletal main.m that encapsulates the rest of the story.

For the most part, the examples for this book use a single application identifier: com.sadun.helloworld. This book uses one identifier to avoid clogging up your iOS devices with dozens of examples at once. Each example replaces the previous one, ensuring that your home screen remains relatively uncluttered. If you want to install several examples simultaneously, simply edit the identifier, adding a unique suffix, such as com.sadun.helloworld.table-edits. You can also edit the custom display name to make the apps visually distinct. Your Team Provisioning Profile matches every application identifier, including com.sadun.helloworld. This enables you to install compiled code to devices without having to change the identifier; just make sure to update your signing identity in each project’s build settings.

Getting the Sample Code

You’ll find the source code for this book at github.com/erica/iOS-6-Cookbook on the open-source GitHub hosting site. There, you can find a chapter-by-chapter collection of source code that provides working examples of the material covered in this book. Recipes are numbered as they are in the book. Recipe 6 in Chapter 5, for example, appears in the C05 folder in the 06 subfolder.

Any project numbered 00 or that has a suffix (such as 05b or 02c) refers to material used to create in-text coverage and figures. Normally I delete these extra projects. Early readers of this manuscript requested that I include them in this edition. You can find a half dozen or so of these extra samples scattered around the repository.

If you do not feel comfortable using git directly, GitHub offers a download button. It was at the right side of the main page at the time this book was written, about halfway down the first page. It enables you to retrieve the entire repository as a ZIP archive or tarball.

Contribute!

Sample code is never a fixed target. It continues to evolve as Apple updates its SDK and the Cocoa Touch libraries. Get involved. You can pitch in by suggesting bug fixes and corrections as well as by expanding the code that’s on offer. GitHub enables you to fork repositories and grow them with your own tweaks and features, and share those back to the main repository. If you come up with a new idea or approach, let me know. My team and I are happy to include great suggestions both at the repository and in the next edition of this Cookbook.

Getting Git

You can download this Cookbook’s source code using the git version control system. An OS X implementation of git is available at http://code.google.com/p/git-osx-installer. OS X git implementations include both command-line and GUI solutions, so hunt around for the version that best suits your development needs.

Getting GitHub

GitHub (http://github.com) is the largest git-hosting site, with more than 150,000 public repositories. It provides both free hosting for public projects and paid options for private projects. With a custom web interface that includes wiki hosting, issue tracking, and an emphasis on social networking of project developers, it’s a great place to find new code or collaborate on existing libraries. You can sign up for a free account at its Web site, enabling you to copy and modify the Cookbook repository or create your own open-source iOS projects to share with others.

Contacting the Author

If you have any comments or questions about this book, please drop me an e-mail message at [email protected], or stop by the github repository and contact me there.

Endnotes

1. No trouts, real or imaginary, were hurt in the development and production of this book. The same cannot be said for countless cans of Diet Coke that selflessly surrendered their contents in the service of this manuscript.

2. See the Cocoa Fundamentals Guide (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaFundamentals.pdf) for a head start on Cocoa, and for Xcode, see A Tour of Xcode (http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/A_Tour_of_Xcode/A_Tour_of_Xcode.pdf).

3. Big Nerd Ranch: www.bignerdranch.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.231.106