100% found this document useful (17 votes)
61 views

Full Download iOS SDK Programming A Beginners Guide 1st Edition James Brannan PDF DOCX

Brannan

Uploaded by

reedsnakshsd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (17 votes)
61 views

Full Download iOS SDK Programming A Beginners Guide 1st Edition James Brannan PDF DOCX

Brannan

Uploaded by

reedsnakshsd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Download the full version of the ebook at ebookname.

com

iOS SDK Programming A Beginners Guide 1st Edition


James Brannan

https://ebookname.com/product/ios-sdk-programming-a-
beginners-guide-1st-edition-james-brannan/

OR CLICK BUTTON

DOWNLOAD EBOOK

Download more ebook instantly today at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

iPhone SDK Programming A Beginner s Guide 5th Edition


James Brannan

https://ebookname.com/product/iphone-sdk-programming-a-beginner-s-
guide-5th-edition-james-brannan/

ebookname.com

Beginning iOS 5 Development Exploring the iOS SDK 1st


Edition Dave Mark

https://ebookname.com/product/beginning-ios-5-development-exploring-
the-ios-sdk-1st-edition-dave-mark/

ebookname.com

Beginning iPhone 4 Development Exploring the iOS SDK 1st


Edition David Mark

https://ebookname.com/product/beginning-iphone-4-development-
exploring-the-ios-sdk-1st-edition-david-mark/

ebookname.com

Digital Cities Architectural Design 07 08 2009 Vol 79 N 4


1st Edition Neil Leach

https://ebookname.com/product/digital-cities-architectural-
design-07-08-2009-vol-79-n-4-1st-edition-neil-leach/

ebookname.com
Labor Relations in the Aviation and Aerospace Industries
1st Edition Robert W. Kaps

https://ebookname.com/product/labor-relations-in-the-aviation-and-
aerospace-industries-1st-edition-robert-w-kaps/

ebookname.com

HTML Professional Projects 4th Edition John W. Gosney

https://ebookname.com/product/html-professional-projects-4th-edition-
john-w-gosney/

ebookname.com

Signals And Systems Second Edition I J Nagrath

https://ebookname.com/product/signals-and-systems-second-edition-i-j-
nagrath/

ebookname.com

American Government Brief edition Ninth edition James Q.


Wilson

https://ebookname.com/product/american-government-brief-edition-ninth-
edition-james-q-wilson/

ebookname.com

Mathematical Modeling of Physical Systems An Introduction


Engineering Technology 1st Edition Basmadjian

https://ebookname.com/product/mathematical-modeling-of-physical-
systems-an-introduction-engineering-technology-1st-edition-basmadjian/

ebookname.com
Radiography of Cultural Material 2nd Edition Andrew
Middleton

https://ebookname.com/product/radiography-of-cultural-material-2nd-
edition-andrew-middleton/

ebookname.com
iOS SDK
Programming:
A Beginner’s Guide
About the Authors
James A. Brannan is a senior developer with more than
15 years of experience. He has developed using everything
from AWK, to Visual Basic, to Java. His current interests
are iOS, Blackberry, Android, and Adobe Flex/Flash. He is
only $999,000 short of being the next app store overnight
millionaire. He lives in Gaithersburg, Maryland, with his wife,
two kids, two Macs, and bicycle.
Blake Ward has a PhD in Computer Science from Carnegie
Mellon University and has spent more than 30 years
programming and managing software development. He has
developed for a wide variety of mobile devices, ranging from the
Apple Newton and Palm Pilot to RIM’s Blackberry, the iPhone
and iPad, and Android phones. Blake has worked as a researcher
and in management at Apple, Xerox PARC, and numerous
startups. He is currently an independent iPhone and Android
developer, available through www.iphoneappquotes.com.

About the Technical Editor


Born to golf, forced to work, Steven Weber, a Java Web
Applications engineer, has ten years application development
under his belt. He’s dabbled in iOS application development
and released one corporate application since the launch of
Apple’s App Store. He’s currently living it up in the Colorado
Rockies.
iOS SDK
Programming:
A Beginner’s Guide

James A. Brannan
Blake Ward

New York Chicago San Francisco


Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto
Copyright © 2011 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no
part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior
written permission of the publisher.

ISBN: 978-0-07-175909-0

MHID: 0-07-175909-3

The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-175908-3,
MHID: 0-07-175908-5.

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we
use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such
designations appear in this book, they have been printed with initial caps.

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training
programs. To contact a representative please e-mail us at bulksales@mcgraw-hill.com.

Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechanical
error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any information and
is not responsible for any errors or omissions or the results obtained from the use of such information.

TERMS OF USE

This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGrawHill”) and its licensors reserve all rights in and to the work. Use of
this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work,
you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate,
sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial
and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these
terms.

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE
ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY
INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM
ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the
work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to
you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill
has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors
be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work,
even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever
whether such claim or cause arises in contract, tort or otherwise.
For Timothy Hill and Doctor Ronald Holt. This book is not some lofty political or
anthropological treatise, but it was fun writing.
—James

For Bryce—the real author in our family.


—Blake
This page intentionally left blank
Contents at a Glance
1 The iOS Software Development Kit (SDK) ............................... 1
2 A C Refresher ............................................................. 25
3 Just Enough Objective-C: Part One ...................................... 41
4 Just Enough Objective-C: Part Two ...................................... 61
5 Deploying to an iPhone, Debugging, and Testing .......................... 77
6 UIApplication and UIApplicationDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
7 UIView and UIViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8 UITabBar and UITabBarController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
9 UINavigationBar and UINavigationController . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
10 Tables Using UITableView and UITableViewController . . . . . . . . . . . . . . . . . . 185
11 Activity Progress and Alerting Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
12 Controls—Part One: Using Buttons, Sliders, Switches,
and Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

vii
viii iOS SDK Programming: A Beginner’s Guide

13 Controls—Part Two: Using Pickers and Using the Camera . . . . . . . . . . . . . . 295


14 Application Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
15 Property Lists and Archiving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
16 Data Persistence Using SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
17 Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
18 Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
19 Universal Applications for the iPad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Contents
ACKNOWLEDGMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix

1 The iOS Software Development Kit (SDK) ............................... 1


The App Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
The Software Development Kit (SDK) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Paid Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Objective-C, Foundation Framework, Cocoa Touch, and UIKit . . . . . . . . . . . . . . . . . . . . . . 5
Cocoa Touch ................................................................. 6
Foundation Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The iOS Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Memory and Processor Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Small Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Short-Lived Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Manual Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Relevant Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Try This: Getting a Quick Start on iOS Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2 A C Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
C Command-Line Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Try This: Creating a Simple C Program Using Xcode ............................... 27

ix
x iOS SDK Programming: A Beginner’s Guide

C Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Understanding Headers, Import, and Include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Try This: Creating a Header File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Preprocessor Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Try This: Using Preprocessor Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Data Types and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Control, Functions, and Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Arrays and Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The printf Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Pointers ............................................................................ 36
Try This: Using Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Dereferencing a Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Try This: Using an Array with Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3 Just Enough Objective-C: Part One ...................................... 41
Objective-C Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Class Interface and Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Try This: Generating an Objective-C Class’ Interface and Implementation . . . . . . . . . . . 43
The @interface and @implementation Compiler Directives . . . . . . . . . . . . . . . . . . . . . . . . . 44
Method Declaration and Definition ................................................. 44
Try This: Adding SayHello to the Simple Class ..................................... 45
Interface Anatomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Implementation Anatomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Public, Private, and Protected Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Understanding Simple Messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Using self in a Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Nested Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Class and Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Try This: Adding sayGoodBye as a Class Method .................................. 51
The alloc and init Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Managing Memory Using Retain and Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Try This: Using Manual Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Instance Variables and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Managing Memory Using Autorelease . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4 Just Enough Objective-C: Part Two ...................................... 61
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Retain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Assign . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Releasing Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Contents xi

Multiple-Argument Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Try This: Creating a Simple Multiple-Argument Message . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Understanding the id Variable Type, Dynamic Typing, and Dynamic Binding . . . . . . . . 69
The id Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Dynamic Binding and Dynamic Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Understanding Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Overriding Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Overloading Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Using Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Using Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
5 Deploying to an iPhone, Debugging, and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Installing Applications on an iPhone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Certificates, Devices, Application IDs, and Provisioning ...................... 80
Try This: Deploying an Application to iPhone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Using the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Try This: Debugging an Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
NSZombieEnabled ........................................................... 97
Try This: Enabling Zombies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Instruments—Leaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Try This: Find a Memory Leak . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Distributing Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Ad Hoc Deployment and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Distributing Your App via the App Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6 UIApplication and UIApplicationDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Try This: Adding a UIView and UIViewController to a UIApplicationDelegate . . . . . . 110
Connecting UIWindow, UIApplication, and UIApplicationDelegate . . . . . . . . . . . . . . . . . 117
Try This: Exploring Main Window.xib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
UIApplication and UIApplicationDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
The main.m File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Handling Application Life Cycle Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Application Interruptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Try This: Handling Application Interruptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
7 UIView and UIViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
The UIView Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
The UIViewController Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
View-Based Application Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
xii iOS SDK Programming: A Beginner’s Guide

IBOutlet and IBAction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130


Try This: Using a View-Based Application Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Try This: Using a Window-Based Application Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
UIViewController and Application Life Cycle Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Try This: Exploring Several Life Cycle Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
8 UITabBar and UITabBarController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
UITabBar, UITabBarController, UITabBarItem, and UITabBarControllerDelegate . . . . 142
Try This: Using the Tab Bar Application Template .................................. 145
Try This: Adding a Tab Bar Item to a Tab Bar Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Try This: Creating a Tab Bar Application from Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Try This: Allowing Users to Customize a Tab Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
9 UINavigationBar and UINavigationController . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
UINavigationBar, UINavigationController, and UINavigationItem .................. 162
Try This: Building a Three-View Application Using a Navigation Bar . . . . . . . . . . . . . . . 163
Adding Another View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Try This: See How a Utility Application Uses NavigationBar . . . . . . . . . . . . . . . . . . . . . . . . 176
More on the UINavigationController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Popping View Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Configuring the Navigation Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Try This: Using a Navigation Controller in a Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
10 Tables Using UITableView and UITableViewController . . . . . . . . . . . . . . . . . . 185
UITableView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
UITableViewDelegate and UITableViewDataSource ................................ 189
UITableViewDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
UITableViewDataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Try This: Adopting the UITableViewDelegate and UITableViewDataSource . . . . . . . . . 191
Try This: Adding a Delegate and Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
UITableViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Try This: Using a UITableViewController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Grouping and Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Grouped Table Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Try This: Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Try This: Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Images in Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Try This: Adding an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Selecting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Try This: Row Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Changing Row Height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Try This: Changing Row Height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Contents xiii

Accessorizing Table Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211


Try This: Accessorizing a Table Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Customizing a Table Cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Try This: Customizing a Cell Using Interface Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Using Tables with Navigation Bars and Tabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Try This: Using a Table in a Navigation Controller in a Tab ......................... 219
Editing Table Cells ................................................................. 226
Getting to Edit Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Edit Mode Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Try This: Editing Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
11 Activity Progress and Alerting Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Showing Activity—the UIActivityIndicatorView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Try This: Using a UIActivitylndicatorView ......................................... 241
Showing Progress—the UIProgressView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Try This: Using a UIProgress View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Alerting Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
UIAlertView and UIAlertViewDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Try This: Creating a Simple UIAlertView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Try This: Using an Alert with Multiple Buttons ..................................... 256
UIActionSheet and UIActionSheetDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Try This: Using a UIActionSheet ................................................... 257
Application Badges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Try This: Adding an Application Badge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
12 Controls—Part One: Using Buttons, Sliders, Switches,
and Text Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
UIButton with a Background Image and Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Try This: Using a Custom Button Background Image and Image .................... 266
Button Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
UIToolBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Try This: Creating a UIToolbar ..................................................... 273
UISwitch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
UISlider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Appearance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Continuous Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Try This: Using a Switch and a Slider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
UITextField ........................................................................ 280
Try This: Using UITextField (with a Number Pad) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
UITextView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
UISegmentedControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
xiv iOS SDK Programming: A Beginner’s Guide

Try This: Using a UISegmentedControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287


The Web View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
UIWebView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
UIWebViewDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Try This: Creating a Simple Web Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
13 Controls—Part Two: Using Pickers and Using the Camera . . . . . . . . . . . . . . 295
Using Pickers: Date Pickers and Pickers ............................................ 296
Date Pickers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Try This: Using a Date Picker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Try This: Using a UIDatePicker in Timer Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
UIPickerView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Try This: Using a Picker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Try This: Using a UIPickerView with Two Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Try This: Loading UIImageViews into a UIPickerView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Using the Camera: UIImagePickerController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
UIImagePickerController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
UIImagePickerControllerDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Try This: Using the UIImagePickerController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Try This: Using Notifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
14 Application Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
The Settings Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
The Settings Bundle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Try This: Creating a Settings Bundle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Settings Field Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Try This: Adding a PSTextFieldSpecifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
PSMultiValueSpecifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Try This: Adding a PSMultiValueSpecifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
PSToggleSwitchSpecifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Try This: Adding a PSToggleSwitchSpecifier ....................................... 340
PSSliderSpecifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Try This: Adding a PSSliderSpecifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
PSChildPaneSpecifier ........................................................ 344
Try This: Adding a PSChildPaneSpecifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Reading Settings Using NSUserDefaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Try This: Reading the Settings Bundle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Changed Settings While Suspended . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
15 Property Lists and Archiving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
An iOS Application’s Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Contents xv

Property Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351


Simple Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Try This: Preserving an NSArray ................................................... 352
NSPropertyListSerialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Try This: Preserving to an XML Property List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Archiving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Protocols to Adopt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
NSKeyedArchiver and NSKeyedUnarchiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Try This: Archiving and Unarchiving an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Try This: Archiving and Unarchiving an Object Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Multitasking and Saving Application State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
16 Data Persistence Using SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Adding a SQLite Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Try This: Creating a Simple Database Using FireFox SQLite Manager .............. 372
Basic SQLite Database Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Opening the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Statements, Preparing Statements, and Executing Statements . . . . . . . . . . . . . . . . . . 377
Select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Try This: Opening and Querying a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
SQLite Binding, Inserting, Updating, and Deleting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Try This: Inserting, Updating, and Deleting Records ................................ 388
Try This: Inserting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Try This: Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Try This: Deleting Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
17 Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Core Data in Brief . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Creating a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Try This: Adding Entities and Relationships to a Core Data Model . . . . . . . . . . . . . . . . . . 403
Model, Context, and Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
NSManagedObjectModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
NSPersistentStoreCoordinator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
NSManagedObjectContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
NSManagedObject ........................................................... 409
NSFetchedResultsController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
NSFetchRequest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
NSPredicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
NSSortDescriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Try This: Fetching All AKCGroup Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
xvi iOS SDK Programming: A Beginner’s Guide

Adding Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415


Saving Changes .................................................................... 415
Deleting Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Updating Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Try This: Adding Navigation and AKCGroup Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Try This: Adding Navigation and Editing for a List of Breeds . . . . . . . . . . . . . . . . . . . . . . . 424
Try This: Adding a Breed Detail View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Distributing Core Data with Your App .............................................. 430
What Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
18 Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Playing Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
AudioServicesPlaySystemSound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
AVAudioPlayer and AVAudioPlayerDelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Try This: Playing a Sound and an MP3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Media Player Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Media Data Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Selecting Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Playing Multimedia: MPMusicPlayerController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Try This: Using the Media Picker and Media Player . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
MPMoviePlayerController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Try This: Play a Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
19 Universal Applications for the iPad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Creating a Universal Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Try This: Building an App for iPad and iPhone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Handling Orientation Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Try This: Reacting to Orientation Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Icons and Default Screens .................................................... 464
Split Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Try This: Add a Split View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Other iPad Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Using Popovers for Information or Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Movies in a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Try This: MoviePlayer Centered on the iPad Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
External Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Working with Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Acknowledgments
T hanks to the technical editor, Steven Weber, and everybody at McGraw-Hill, particularly
Roger Stewart, Joya Anthony, Jody McKenzie, Vastavikta Sharma, and Bob Campbell.
Special thanks to Everaldo and his Crystal Project Icons licensed under the LGPL. These
icons have made the examples much more visually appealing in both this book and others.
And of course, thanks to Neil Salkind, our book agent, who introduced us to computer book
writing and kept the ship navigating straight despite some stormy moments. Finally, thanks to
the iPhone SDK Forum (www.iphonesdk.com). In no small part, the idea for a tutorial-based
approach for this book came directly from your video tutorials offered on your site.

xvii
This page intentionally left blank
Introduction
R esponse to the iPhone, the iPod touch, and now the iPad has been nothing short of
overwhelming. The App Store has captured the hobbyist’s imagination like no other
platform in recent memory. Hobbyists have made—and will continue to make—money from
their creations sold on the App Store. And we aren’t necessarily talking about high-minded
technical innovations. The media has reported that apps that make your iPhone pass gas have
made folks hundreds of thousands of dollars. Rival farting App developers have even gone so
far as to sue one another over the App Store’s precious revenue. The iOS family of devices
and the App Store are here to stay—and there’s still plenty of opportunity for you to create
the next great app.
As proof of this popularity, after posting a few tutorial videos on Vimeo, James heard
from people from Asia, Europe, and South America about those videos. So, when we decided
to write this book, we kept in mind that iOS devices have significant international appeal.
We have tried to make this book as accommodating as possible for as wide an audience as
possible. We have kept colloquialisms to a minimum, for instance. But more important than
avoiding colloquialisms, this book relies upon discrete, numbered steps that illustrate each
major concept. Rather than a lot of prose describing the iOS SDK, we show you the SDK in
action.

The Book’s Focus


This book has three goals. The first of these is to get you comfortable with using the iOS’s user
interface controls in Interface Builder. Interface Builder is a useful tool that removes much
of the complexity of creating and laying out the user interface for iOS apps. Once you master

xix
xx iOS SDK Programming: A Beginner’s Guide

this tool, building a graphical user interface (GUI) using Interface Builder is quicker and more
intuitive than using code.
The second goal of this book is to brush up your C language programming skills and
introduce you to Objective-C. Most likely you haven’t used C since college, and chances are
good you have never used Objective-C. However, because Objective-C is the language used
for Cocoa and Cocoa Touch programming, you must understand Objective-C if you wish
to create iPhone and iPad apps. After refreshing your memory on C, this book moves on to
Objective-C with a two-chapter tutorial that will give you a foundation for getting started with
the iOS SDK.
The third goal of this book is to cover all of the most useful functionality of the iOS SDK
so that you’re ready to create your own iOS apps for the App Store. We cover using the latest
version of Apple’s development environment, XCode 4, and most of the features introduced in
the latest versions of the SDK for the iPad and the iPhone 4.

NOTE
This book’s code examples can be downloaded at:
www.mhprofessional.com/computingdownloads.

The Book’s Content


This book assumes no prior C or Objective-C knowledge. Although not comprehensive,
chapters on C and Objective-C should provide enough detail to understand the book’s
remaining chapters. The book starts with the prerequisites. Both C and Objective-C are
prerequisites to programming iOS applications. You don’t need to be a C expert to use
Objective-C, but you should remember C’s basics. After providing a C refresher, the book
has two chapters on Objective-C. These chapters introduce a lot of concepts quickly, but
Objective-C is the language used for Cocoa Touch, so you’d be advised to learn it. After
covering Objective-C, the book provides a chapter on installing an iOS application on an
iPhone, iPod touch, or iPad device. It also provides a tutorial on debugging and testing your
application.
Chapter 6 finally begins the book’s UIKit coverage. Chapters 6 through 10 discuss the
UIView subclasses you use when laying out an iOS application. Chapter 11 discusses alerts,
action sheets, and application badges. Chapters 12 and 13 discuss the many controls available
for an iOS user interface. Chapter 13 also discusses how to use the photo library and the
camera built into the iPhone.
After describing the UIKit, the book then moves on to discuss several other essential iOS
application programming topics. Chapter 14 discusses setting your application’s preferences
using the Settings application. Chapter 15 discusses file I/O, property lists, and archiving
objects. Chapter 16 discusses using the iOS’s built-in database, SQLite. Chapter 17 builds
a more complex iOS application and discusses Core Data, by far the easiest persistence
framework you can use while programming with iOS. Chapter 18 discusses using iTunes
music in your application.
Introduction xxi

Finally, Chapter 19 discusses the new SDK functionality available for the iPad and shows
you how to create a universal application that will run on the iPhone or iPod touch but also
take full advantage of the larger display on the iPad when available. All of the framework
functionality described in the earlier chapters applies to the iPad and the new iPhone 4, so
this chapter focuses on how to layer new iOS 4 functionality on top of the skills that you’ve
already learned.
This book doesn’t require any prior knowledge of C, Objective-C, or Cocoa Touch and
the iOS frameworks, so provided you have some prior programming experience and you
work through all of the exercises in the book, you should be ready to start working on your
own iPhone applications when you’ve finished the book. However, even if you eventually
decide to hire an independent developer to help build your application (via a web site like
www.iphoneappquotes.com), everything you’ve learned working through the exercises will
be invaluable when it comes time to turn your ideas into a design and work with others to
implement them.
This page intentionally left blank
Chapter 1
The iOS Software
Development Kit (SDK)

1
2 iOS SDK Programming: A Beginner’s Guide

Key Skills & Concepts


L Understanding the App Store
L Understanding how to obtain Xcode and the iOS SDK
L Deciding if this book is right for you
L Understanding Xcode’s help and Apple’s online documentation

S o why do people pay over $100 a month for an iPhone? Or more than $500 for an iPad?
Simple—they’re useful tools and fun toys. If you get lost, just start the Maps application,
and within seconds, it has located your position and provided you with a map. You can check
your e-mail anywhere, listen to music, and every once in a while even answer a phone call.
The built-in functionality of the iPhone, iPod Touch, and iPad is undeniably useful, but the real
magic of these devices is the App Store. There you can find more than a quarter of a million
applications that turn your iPhone from a useful general device to a tool specialized for exactly
what you want to do.
Apple’s App Store has created a new phenomenon—millions of people think of buying
cheap apps the same way they think about picking up a latte on the way to work; it’s an impulse
buy they do several times a week. Unlike other smartphone users, iPhone users buy apps, lots of
them! There may already be a staggering number of apps in the App Store, but the opportunities
are still endless for turning your ideas into profitable apps.

NOTE
Apple reviews every app before publication in the App Store, and you should read
their current guidelines for acceptance before starting on your app. Obvious categories
like gambling and pornography aren’t allowed, but even apps that show scantily clad
models risk rejection from the App Store. But don’t worry too much; if your app is bug
free and follows the guidelines, it will probably be approved within a week.

The App Store


The App Store is a unique concept. The App Store is an Apple application on the iPhone, iPod
Touch, and iPad. You use the App Store to browse and download applications from Apple’s
iTunes Store. Some applications are free, while others have a (usually) nominal charge. Using
your iTunes account, you can download applications directly to your iPhone, iPod Touch, or
iPad (your device). What we like is that anyone can use an iTunes Gift Card that you buy at
your local grocery store; no credit card needed.
Random documents with unrelated
content Scribd suggests to you:
then, after a walk in the fine gardens, we went to Mrs. Browne’s,
where Sir W. Pen and I were godfathers, and Mrs. Jordan and
Shipman godmothers to her boy. And there, before and after the
christening, we were with the woman above in her chamber; but
whether we carried ourselves well or ill, I know not; but I was
directed by young Mrs. Batten. One passage of a lady that ate
wafers with her dog did a little displease me. I did give the midwife
10s. and the nurse 5s. and the maid of the house 2s. But for as
much I expected to give the name to the child, but did not (it being
called John), I forbore then to give my plate.
December 26th, 1662.—Up, my wife to the making of Christmas
pies all day, doeing now pretty well again, and I abroad to several
places about some businesses, among others bought a bake-pan in
Newgate Market, and sent it home, it cost me 16s. So to Dr Williams,
but he is out of town, then to the Wardrobe. Hither come Mr
Battersby; and we falling into discourse of a new book of drollery in
use, called Hudibras, I would needs go find it out, and met with it at
the Temple: cost me 2s. 6d. But when I come to read it, it is so silly
an abuse of the Presbyter Knight going to the warrs, that I am
ashamed of it; and by and by meeting at Mr Townsend’s at dinner, I
sold it to him for 18d. ...
February 6th.— ... Thence to Lincoln’s Inn Fields; and it being too
soon to go to dinner, I walked up and down, and looked upon the
outside of the new theatre now a-building in Covent Garden, which
will be very fine. And so to a bookseller’s in the Strand, and there
bought Hudibras again, it being certainly some ill-humour to be so
against that which all the world cries up to be the example of wit; for
which I am resolved once more to read him, and see whether I can
find it or no....
November 28th.— ... And thence abroad to Paul’s Churchyard,
and there looked upon the second part of Hudibras, which I buy not,
but borrow to read, to see if it be as good as the first, which the world
cry so mightily up, though it hath not a good liking in me, though I
had tried by twice or three times reading to bring myself to think it
witty. Back again and home to my office....
May 11th, 1667.—And so away with my wife, whose being
dressed this day in fair hair did make me so mad, that I spoke not
one word to her, though I was ready to burst with anger.... After that
... Creed and I into the Park, and walked, a most pleasant evening,
and so took coach, and took up my wife, and in my way home
discovered my trouble to my wife for her white locks [false hair],
swearing by God several times, which I pray God forgive me for, and
bending my fist, that I would not endure it. She, poor wretch, was
surprized with it, and made me no answer all the way home; but
there we parted, and I to the office late, and then home, and without
supper to bed, vexed.
12th (Lord’s Day).—Up and to my chamber, to settle some
accounts there, and by and by down comes my wife to me in her
night-gown, and we begun calmly, that, upon having money to lace
her gown for second mourning, she would promise to wear white
locks no more in my sight, which I, like a severe fool, thinking not
enough, began to except against, and made her fly out to very high
terms and cry, and in her heat told me of keeping company with Mrs
Knipp, saying, that if I would promise never to see her more—of
whom she hath more reason to suspect than I had heretofore of
Pembleton—she would never wear white locks more. This vexed
me, but I restrained myself from saying anything, but do think never
to see this woman—at least, to have her here more; but by and by I
did give her money to buy lace, and she promised to wear no more
white locks while I lived, and so all very good friends as ever, and I to
my business, and she to dress herself.
August 18th (Lord’s Day).—Up, and being ready, walked up and
down to Cree Church, to see it how it is: but I find no alteration there,
as they say there was, for my Lord Mayor and Aldermen to come to
sermon, as they do every Sunday, as they did formerly to Paul’s....
There dined with me Mr Turner and his daughter Betty. Betty is
grown a fine young lady as to carriage and discourse. I and my wife
are mightily pleased with her. We had a good haunch of venison,
powdered and boiled, and a good dinner and merry.... I walked
towards Whitehall, but, being wearied, turned into St Dunstan’s
Church, where I heard an able sermon of the minister of the place;
and stood by a pretty, modest maid, whom I did labour to take by the
hand ...; but she would not, but got further and further from me; and,
at last, I could perceive her to take pins out of her pocket to prick me
if I should touch her again—which seeing, I did forbear, and was glad
I did spy her design. And then I fell to gaze upon another pretty maid,
in a pew close to me, and she on me; and I did go about to take her
by the hand, which she suffered a little, and then withdrew. So the
sermon ended, and the church broke up.

John Dryden, famous alike for his verse, prose and drama, shows
his wit in biting, stinging satire.
Equally caustic are his epigrams, save one—the immortal lines on
Milton.
ON SHADWELL
All human things are subject to decay,
And, when Fate summons, monarchs must obey.
This Flecknoe found, who, like Augustus, young
Was called to empire, and had governed long.
In prose and verse was owned, without dispute,
Through all the realms of Nonsense absolute.
This aged prince, now flourishing in peace,
And blest with issue of a large increase,
Worn out with business, did at length debate
To settle the succession of the state;
And pondering which of all his sons was fit
To reign, and wage immortal war with Wit,
Cried: “’Tis resolved; for Nature pleads that he
Should only rule who most resembles me.
Shadwell alone my perfect image bears,
Mature in dulness from his tender years;
Shadwell alone of all my sons is he
Who stands confirmed in full stupidity.
The rest to some faint meaning make pretence,
But Shadwell never deviates into sense.
Some beams of wit on other souls may fall,
Strike through, and make a lucid interval,
But Shadwell’s genuine night admits no ray;
His rising fogs prevail upon the day.
Besides, his goodly fabric fills the eye,
And seems designed for thoughtless majesty—
Thoughtless as monarch oaks that shade the plain,
And, spread in solemn state, supinely reign.”

ON THE DUKE OF BUCKINGHAM


Some of their chiefs were princes of the land:
In the first rank of these did Zimri stand,
A man so various, that he seemed to be
Not one, but all mankind’s epitome:
Stiff in opinions, always in the wrong,
Was everything by starts, and nothing long,
But, in the course of one revolving moon,
Was chemist, fiddler, statesman, and buffoon,
Then all for women, painting, rhyming, drinking,
Besides ten thousand freaks that died in thinking.
Blest madman, who could every hour employ
With something new to wish or to enjoy,
Railing, and praising, were his usual themes;
And both, to show his judgment, in extremes:
So over-violent, or over-civil,
That every man with him was god or devil.
In squandering wealth was his peculiar art;
Nothing went unrewarded but desert.
Beggared by fools, whom still he found too late,
He had his jest and they had his estate.
He laughed himself from court, then sought relief
By forming parties, but could ne’er be chief;
For spite of him, the weight of business fell
On Absalom and wise Achitophel.
Thus, wicked but in will, of means bereft,
He left not faction, but of that was left.

MILTON COMPARED WITH HOMER AND VIRGIL


Under a Picture of Milton in the 4th Edition of Paradise Lost.
Three Poets, in three distant ages born,
Greece, Italy, and England did adorn.
The first, in loftiness of thought surpass’d
The next, in majesty; in both the last.
The force of nature could no further go;
To make a third, she join’d the former two.

The original of these fine lines was probably a Latin distich written
by Selvaggi at Rome, which has been thus translated:
Greece boasts her Homer, Rome her Virgil’s name,
But England’s Milton vies with both in fame.

Cowper’s lines on Milton may be compared with Dryden’s:


Ages elapsed ere Homer’s lamp appear’d,
And ages ere the Mantuan Swan was heard
To carry Nature lengths unknown before,
To give a Milton birth, ask’d ages more.
Thus Genius rose and set at order’d times,
And shot a day-spring into distant climes,
Ennobling every region that he chose;
He sunk in Greece, in Italy he rose;
And, tedious years of gothic darkness pass’d,
Emerged all splendour in our isle at last,
Thus lovely halcyons dive into the main,
Then show far off their shining plumes again.

In Bishop Gibson’s edition of Camden’s Britannia, there is a very


free translation of some old monkish verses on S. Oswald by Basil
Kennet, brother of Bishop White Kennet. The last line, to which there
is nothing corresponding in the Latin, seems to have been copied
from the last line of Dryden’s epigram:
Cæsar and Hercules applaud thy fame,
And Alexander owns thy greater name,
Tho’ one himself, one foes, and one the world o’ercame:
Great conquests all! but bounteous Heav’n in thee,
To make a greater, join’d the former three.

The comedies of William Congreve, brilliantly witty though they


are, offer no suitable passages to quote.
Likewise the works of Daniel Defoe, who, beside the story of
Robinson Crusoe, wrote satirical humor.

FROM ROBINSON CRUSOE


Friday’s Conflict with the Bear

But never was a fight managed so hardily, and in such a


surprising manner, as that between Friday and the bear, which gave
us all—though at first we were surprised and afraid for him—the
greatest diversion imaginable.
My man Friday had delivered our guide, and when we came up to
him he was helping him off from his horse, for the man was both hurt
and frightened, and indeed the last more than the first, when on a
sudden we espied the bear come out of the wood, and a vast,
monstrous one it was, the biggest by far that ever I saw. We were all
a little surprised when we saw him; but when Friday saw him, it was
easy to see joy and courage in the fellow’s countenance. “Oh, oh,
oh!” says Friday three times, pointing to him; “oh, master! you give
me te leave, me shakee te hand with him; me makee you good
laugh.”
I was surprised to see the fellow so pleased. “You fool!” said I, “he
will eat you up.” “Eatee me up! eatee me up!” says Friday twice over
again; “me eatee him up; me makee you good laugh; you all stay
here, me show you good laugh.” So down he sits, and gets his boots
off in a moment, and puts on a pair of pumps (as we call the flat
shoes they wear, and which he had in his pocket), gives my other
servant his horse, and with his gun away he flew, swift like the wind.
The bear was walking softly on, and offered to meddle with
nobody, till Friday, coming pretty near, calls to him as if the bear
could understand him, “Hark ye, hark ye,” says Friday, “me speakee
with you.” We followed at a distance, for now, being come down to
the Gascony side of the mountains, we were entered a vast, great
forest, where the country was plain and pretty open, though it had
many trees in it scattered here and there. Friday, who had, as we
say, the heels of the bear, came up with him quickly, and took up a
great stone and threw it at him, and hit him just on the head, but did
him no more harm than if he had thrown it against a wall; but it
answered Friday’s end, for the rogue was so void of fear that he did
it purely to make the bear follow him and show us some laugh, as he
called it. As soon as the bear felt the stone, and saw him, he turns
about and comes after him, taking very long strides, and shuffling on
at a strange rate, so as would have put a horse to a middling gallop.
Away runs Friday, and takes his course as if he ran toward us for
help; so we all resolved to fire at once upon the bear, and deliver my
man; though I was angry at him heartily for bringing the bear back
upon us, when he was going about his own business another way;
and especially I was angry that he had turned the bear upon us and
then run away; and I called out, “You dog!” said I, “is this your
making us laugh? Come away, and take your horse, that we may
shoot the creature.” He heard me, and cried out, “No shoot! no
shoot! stand still, you get much laugh.” And as the nimble creature
ran two feet for the beast’s one, he turned on a sudden on one side
of us, and seeing a great oak-tree fit for his purpose, he beckoned us
to follow; and doubling his pace, he got nimbly up the tree, laying his
gun down upon the ground, at about five or six yards from the
bottom of the tree.
The bear soon came to the tree, and we followed at a distance.
The first thing he did, he stopped at the gun, smelled at it, but let it
lie, and up he scrambles into the tree, climbing like a cat, though so
monstrous heavy. I was amazed at the folly, as I thought it, of my
man, and could not for my life see anything to laugh at yet, till,
seeing the bear get up the tree, we all rode near to him.
When we came to the tree, there was Friday got out to the small
end of a large limb of the tree, and the bear got about half-way to
him. As soon as the bear got out to that part where the limb of the
tree was weaker, “Ha!” says he to us, “now you see me teachee the
bear dance”; so he began jumping and shaking the bough, at which
the bear began to totter, but stood still, and began to look behind
him, to see how he should get back; then, indeed, we did laugh
heartily. But Friday had not done with him by a great deal. When
seeing him stand still, he called out to him again, as if he had
supposed the bear could speak English, “What, you no come
farther? Pray you come farther.” So he left jumping and shaking the
bough; and the bear, just as if he had understood what he had said,
did come a little farther. Then he began jumping again, and the bear
stopped again. We thought now was a good time to knock him on
the head, and called to Friday to stand still, and we would shoot the
bear; but he cried out earnestly, “Oh, pray! oh, pray! no shoot! me
shoot by-and-then.” He would have said by-and-by.
However, to shorten the story, Friday danced so much, and the
bear stood so ticklish, that we had laughing enough indeed, but still
could not imagine what the fellow would do; for first we thought he
depended upon shaking the bear off; and we found the bear was too
cunning for that too; for he would not go out far enough to be thrown
down, but clung fast with his great broad claws and feet, so that we
could not imagine what would be the end of it, and what the jest
would be at last. But Friday put us out of doubt quickly; for, seeing
the bear cling fast to the bough, and that he would not be persuaded
to come any farther, “Well, well,” says Friday, “you no come farther,
me go; you no come to me, me come to you.” And upon this he went
out to the smaller end of the bough, where it would bend with his
weight, and gently let himself down by it, sliding down the bough till
he came near enough to jump down on his feet, and away he ran to
his gun, took it up, and stood still. “Well,” said I to him, “Friday, what
will you do now? Why don’t you shoot him?” “No shoot,” says Friday,
“no yet; me shoot now, me no kill; me stay, give you one more
laugh.” And, indeed, so he did, as you will see presently. For when
the bear saw his enemy gone, he came back from the bough where
he stood, but did it very cautiously, looking behind him every step,
and coming backward till he got into the body of the tree. Then, with
the same hinder end foremost, he came down the tree, grasping it
with his claws, and moving one foot at a time, very leisurely. At this
juncture, and just before he could set his hind feet upon the ground,
Friday stepped up close to him, clapped the muzzle of his piece into
his ear, and shot him dead as a stone. Then the rogue turned about
to see if we did not laugh; and when he saw we were pleased by our
looks, he began to laugh very loud. “So we kill bear in my country,”
says Friday. “So you kill them?” says I; “why, you have no guns.”
“No,” says he, “no gun, but shoot great much long arrow.”
Matthew Prior was called by Thackeray the most charmingly
humorous of the English poets, and Cowper speaks of Prior’s
charming ease.
AN EPITAPH
Interred beneath this marble stone
Lie sauntering Jack and idle Joan.
While rolling threescore years and one
Did round this globe their courses run.
If human things went ill or well,
If changing empires rose or fell,
The morning past, the evening came,
And found this couple just the same.
They walked and ate, good folks. What then?
Why, then they walked and ate again;
They soundly slept the night away;
They did just nothing all the day,
Nor sister either had, nor brother;
They seemed just tallied for each other.
Their moral and economy
Most perfectly they made agree;
Each virtue kept its proper bound,
Nor trespassed on the other’s ground.
Nor fame nor censure they regarded;
They neither punished nor rewarded.
He cared not what the footman did;
Her maids she neither praised nor chid;
So every servant took his course,
And, bad at first, they all grew worse;
Slothful disorder filled his stable.
And sluttish plenty decked her table.
Their beer was strong, their wine was port;
Their meal was large, their grace was short.
They gave the poor the remnant meat,
Just when it grew not fit to eat.
They paid the church and parish rate,
And took, but read not, the receipt:
For which they claimed their Sunday’s due
Of slumbering in an upper pew.
No man’s defects sought they to know,
So never made themselves a foe.
No man’s good deeds did they commend,
So never raised themselves a friend.
Nor cherished they relations poor,
That might decrease their present store;
Nor barn nor house did they repair,
That might oblige their future heir.
They neither added nor confounded;
They neither wanted nor abounded.
Nor tear nor smile did they employ
At news of grief or public joy
When bells were rung and bonfires made,
If asked, they ne’er denied their aid;
Their jug was to the ringers carried,
Whoever either died or married
Their billet at the fire was found,
Whoever was deposed or crowned.
Nor good, nor bad, nor fools, nor wise;
They would not learn, nor could advise;
Without love, hatred, joy, or fear,
They led—a kind of—as it were;
Nor wished, nor cared, nor laughed, nor cried.
And so they lived, and so they died.
A SIMILE
Dear Thomas, didst thou never pop
Thy head into a tin-man’s shop?
There, Thomas, didst thou never see
(’Tis but by way of simile)
A squirrel spend his little rage,
In jumping round a rolling cage?
The cage, as either side turned up,
Striking a ring of bells a-top?—
Mov’d in the orb, pleas’d with the chimes,
The foolish creature thinks he climbs:
But here or there, turn wood or wire,
He never gets two inches higher.
So fares it with those merry blades,
That frisk it under Pindus’ shades.
In noble songs, and lofty odes,
They tread on stars, and talk with gods;
Still dancing in an airy round,
Still pleased with their own verses’ sound;
Brought back, how fast soe’er they go,
Always aspiring, always low.

PHILLIS’ AGE
How old may Phillis be, you ask,
Whose beauty thus all hearts engages?
To answer is no easy task:
For she has really two ages.

Stiff in brocade, and pinch’d in stays,


Her patches, paint and jewels on;
All day let envy view her face,
And Phillis is but twenty-one.

Paint, patches, jewels laid aside,


At night astronomers agree,
The evening has the day belied;
And Phillis is some forty-three.

Prior delighted in epigrams on ladies who wore false hair and


teeth, and who attempted to retain the beauty of youth by means of
paint and dye. They are generally imitated from Martial.
A REASONABLE AFFLICTION
In a dark corner of the house
Poor Helen sits, and sobs, and cries;
She will not see her loving spouse,
Nor her more dear picquet allies:
Unless she find her eye-brows,
She’ll e’en weep out her eyes.

FRENCH HUMOR
The first French humorist of note in the seventeenth century was
Cyrano de Bergerac. His History of the Moon and History of the Sun
are of the nature of Gulliver’s Travels.

THE SOUL OF THE CABBAGE

We laid ourselves along upon very soft quilts, covered with large
carpets; and a young man that waited on us, taking the oldest of our
philosophers led him into a little parlor apart, where my Spirit called
to him to come back to us as soon as he had supped.
This humor of eating separately gave me the curiosity of asking
the cause of it. “He’ll not relish,” said he, “the steam of meat, nor yet
of herbs, unless they die of themselves, because he thinks they are
sensible of pain.” “I wonder not so much,” replied I, “that he abstains
from flesh, and all things that have had a sensitive life. For in our
world the Pythagoreans, and even some holy Anchorites, have
followed that rule; but not to dare, for instance, cut a cabbage, for
fear of hurting it—that seems to me altogether ridiculous.” “And for
my part,” answered my Spirit, “I find a great deal of reason in his
opinion.
“For, tell me is not that cabbage you speak of a being existent in
Nature as well as you? Is not she the common mother of you both?
Yet the opinion that Nature is kinder to mankind than to cabbage-
kind, tickles and makes us laugh. But, seeing she is incapable of
passion, she can neither love nor hate anything; and were she
susceptible of love, she would rather bestow her affection upon this
cabbage, which you grant cannot offend her, than upon that man
who would destroy her if it lay in his power.
“And, moreover, man cannot be born innocent, being a part of the
first offender. But we know very well that the first cabbage did not
offend its Creator. If it be said that we are made after the image of
the Supreme Being, and the cabbage is not—grant that to be true;
yet by polluting our soul, wherein we resembled Him, we have
effaced that likeness, seeing nothing is more contrary to God than
sin. If, then, our soul be no longer His image, we resemble Him no
more in our feet, hands, mouth, forehead, and ears, than a cabbage
in its leaves, flowers, stalk, pith, and head—do not you really think
that if this poor plant could speak when one cuts it, it would not say,
‘Dear brother man, what have I done to thee that deserves death? I
never grow but in gardens, and am never to be found in desert
places, where I might live in security; I disdain all other company but
thine, and scarcely am I sowed in thy garden when, to show thee my
good-will, I blossom, stretch out my arms to thee, offer thee my
children in grain; and, as a requital for my civility, thou causest my
head to be chopped off.’ Thus would a cabbage discourse if it could
speak.
“To massacre a man is not so great sin as to cut and kill a
cabbage, because one day the man will rise again, but the cabbage
has no other life to hope for. By putting to death a cabbage, you
annihilate it; but in killing a man, you make him only change his
habitation. Nay, I’ll go farther with you still: since God doth equally
cherish all His works, and hath equally, divided the benefits betwixt
us and plants, it is but just we should have an equal esteem for them
as for ourselves. It is true we were born first, but in the family of God
there is no birthright. If, then, the cabbage share not with us in the
inheritance of immortality, without doubt that want was made up by
some other advantage, that may make amends for the shortness of
its being—maybe by an universal intellect, or a perfect knowledge of
all things in their causes. And it is for that reason that the wise Mover
of all things hath not shaped for it organs like ours, which are proper
only for simple reasoning, not only weak, but often fallacious too; but
others, more ingeniously framed, stronger, and more numerous,
which serve to conduct its speculative exercises. You’ll ask me,
perhaps, whenever any cabbage imparted those lofty conceptions to
us? But tell me, again, who ever discovered to us certain beings,
which we allow to be above us, to whom we bear no analogy nor
proportion, and whose existence it is as hard for us to comprehend
as the understanding and ways whereby a cabbage expresses itself
to its like, though not to us, because our senses are too dull to
penetrate so far?
“Moses, the greatest of philosophers, who drew the knowledge of
nature from the fountain-head, Nature herself, hinted this truth to us
when he spoke of the Tree of Knowledge; and without doubt he
intended to intimate to us under that figure that plants, in exclusion of
mankind, possess perfect philosophy. Remember, then, oh, thou
proudest of animals, that though a cabbage which thou cuttest
sayeth not a word, yet it pays in thinking. But the poor vegetable has
no fit organs to howl as you do, nor yet to frisk about and weep. Yet
it hath those that are proper to complain of the wrong you do it, and
to draw a judgment from Heaven upon you for the injustice. But if
you still demand of me how I come to know that cabbages and
coleworts conceive such pretty thoughts, then will I ask you, how
come you to know that they do not; and how that some among them,
when they shut up at night, may not compliment one another as you
do, saying, ‘Good-night, Master Cole-Curled-Pate! Your most humble
servant, good Master Cabbage-Round-Head!’”

Marc-Antoine Gerard, sieur de Saint Amant, was one of the


brightest and best of the French early poets.
We give a specimen of his lighter verse. The following is “An
Address to Bacchus:”
In idle rhymes we waste our days,
With yawning fits for all our praise,
While Bacchus, god of mirth and wine,
Invites us to a life divine.
Apollo, prince of bards and prigs,
May scrape his fiddle to the pigs;
And for the Muses, old maids all,
Why let them twang their lyres, and squall
Their hymns and odes on classic themes,
Neglected by their sacred streams.
As for the true poetic fire,
What is it but a mad desire?
While Pegasus himself, at best,
Only a horse must be confess’d;
And he must be an ass indeed,
Who would bestride the winged steed.

Bacchus, thou who watchest o’er


All feasts of ours, whom I adore
With each new draught of rosy wine
That makes my red face like to thine—
By thy ivied coronet,
By this glass with rubies set,
By thy thyrsus—fear of earth—
By thine everlasting mirth,
By the honor of the feast,
By thy triumphs, greatest, least,
By thy blows, not struck, but drunk,
With king and bishop, priest and monk,
By the jesting, keen and sharp,
By the violin and harp,
By the bells, which are but flasks,
By our sighs which are but masks
Of mirth and sacred mystery,
By thy panthers fierce to see,
By this place so fair and sweet,
By the he-goat at thy feet,
By Ariadne, buxom lass,
By Silenus on his ass,
By this sausage, by this stoup,
By this rich and thirsty soup,
By this pipe from which I wave
All the incense thou dost crave,
By this ham, well spiced, long hung,
By this salt and wood-smoked tongue,
Receive us in the happy band
Of those who worship glass in hand.
And, to prove thyself divine,
Leave us never without wine.

Molière (the stage name of Jean Baptiste Poquelin), the greatest


comic dramatist of France, wrote thirty or more plays. Though
difficult to quote significant passages, two are here given:

FROM “THE LEARNED WOMEN”

Trissotin. Your verses have beauties unequaled by any others.


Vadius. Venus and the graces reign in all yours.
Trissotin. You have an easy style, and a fine choice of words.
Vadius. In all your writings one finds ithos and pathos.
Trissotin. We have seen some eclogues of your composition
which surpass in sweetness those of Theocritus and Vergil.
Vadius. Your odes have a noble, gallant, and tender manner,
which leaves Horace far behind.
Trissotin. Is there anything more lovely than your canzonets?
Vadius. Is there anything equal to the sonnets you write?
Trissotin. Is there anything more charming than your little
rondeaus?
Vadius. Anything so full of wit as your madrigals?
Trissotin. If France could appreciate your value——
Vadius. If the age could render justice to a lofty genius——
Trissotin. You would ride in the streets in a gilt coach.
Vadius. We should see the public erect statues to you. Hem—It is
a ballad; and I wish you frankly to——
Trissotin. Have you heard a certain little sonnet upon the Princess
Urania’s fever?
Vadius. Yes; I heard it read yesterday.
Trissotin. Do you know the author of it?
Vadius. No, I do not; but I know very well that, to tell him the truth,
his sonnet is good for nothing.
Trissotin. Yet a great many people think it admirable.
Vadius. It does not prevent it from being wretched; and if you had
read it you would think like me.
Trissotin. I know that I should differ from you altogether, and that
few people are able to write such a sonnet.
Vadius. Heaven forbid that I should ever write one so bad!
Trissotin. I maintain that a better one cannot be made, and my
reason is that I am the author of it.
Vadius. You?
Trissotin. Myself.
Vadius. I cannot understand how the thing could have happened.
Trissotin. It is unfortunate that I had not the power of pleasing you.
Vadius. My mind must have wandered during the reading, or else
the reader spoiled the sonnet; but let us leave that subject, and
come to my ballad.
Trissotin. The ballad is, to my mind, an insipid thing; it is no longer
the fashion, and savors of ancient times.
Vadius. Yet a ballad has charms for many people.
Trissotin. It does not prevent me from thinking it unpleasant.
Vadius. That does not make it worse.
Trissotin. It has wonderful attractions for pedants.
Vadius. Yet we see that it does not please you.
Trissotin. You stupidly impose your qualities on others.
Vadius. You very impertinently cast yours upon me.
Trissotin. Go, you little dunce, you pitiful quill-driver!
Vadius. Go, you penny-a-liner, you disgrace to the profession!
Trissotin. Go, you book-manufacturer, you impudent plagiarist!
Vadius. Go, you pedantic snob!
Philosopher. Ah! gentlemen, what are you about?
Trissotin (to Vadius). Go, go, and make restitution to the Greeks
and Romans for all your shameful thefts!
Vadius. Go, and do penance on Parnassus for having murdered
Horace in your verses!
Trissotin. Remember your book, and the little stir it made.
Vadius. And you, remember your bookseller, reduced to the
workhouse.
Trissotin. My fame is established; in vain would you endeavor to
shake it.
Vadius. Yes, yes; I’ll send you to the author of the Satires.
Trissotin. I, too, will send you to him.
Vadius. I have the satisfaction of having been honorably treated
by him; he gives me a passing thrust, and includes me among
several authors well known at court. But you he never leaves in
peace; in all his verses he attacks you.
Trissotin. By that we see the honorable rank I hold. He leaves you
in the crowd, and esteems one blow enough to crush you. He has
never done you the honor of repeating his attacks, whereas he
assails me separately, as a noble adversary against whom all his
efforts are necessary. His blows, repeated against me on all
occasions, show that he never thinks himself victorious.
Vadius. My pen will teach you what soft of man I am!
Trissotin. And mine will make you know your master!
Vadius. I defy you in verse, prose, Greek, and Latin!
Trissotin. Very well, we shall meet again at the bookseller’s!

FROM “THE GENTLEMAN CIT”


Professor of Philosophy. I will thoroughly explain all these
curiosities to you.
M. Jourdain. Pray do. And now I want to entrust you with a great
secret. I am in love with a lady of quality, and I should be glad if you
would help me to write something to her in a short letter which I
mean to drop at her feet.
Professor of Philosophy. Very well.
M. Jourdain. That will be gallant, will it not?
Professor of Philosophy. Undoubtedly. Is it verse you wish to write
to her?
M. Jourdain. Oh, no, not verse.
Professor of Philosophy. You only wish for prose?
M. Jourdain. No, I wish neither verse nor prose.
Professor of Philosophy. It must be one or the other.
M. Jourdain. Why?
Professor of Philosophy. Because, sir, there is nothing by which
we can express ourselves except prose or verse.
M. Jourdain. There is nothing but prose or verse?
Professor of Philosophy. No, sir. Whatever is not prose is verse,
and whatever is not verse is prose.
M. Jourdain. And when we speak, what is that, then?
Professor of Philosophy. Prose.
M. Jourdain. What! when I say, “Nicole, bring me my slippers, and
give me my night-cap,” is that prose?
Professor of Philosophy. Yes, sir.
M. Jourdain. Upon my word, I have been talking prose these forty
years without being aware of it! I am under the greatest obligation to
you for informing me. Well, then, I wish to write to her in a letter, Fair
marchioness, your beautiful eyes make me die of love! but I would
have this worded in a genteel manner, and turned prettily.
Professor of Philosophy. Say that the fire of her eyes has reduced
your heart to ashes; that you suffer day and night for her tortures
——
M. Jourdain. No, no, no; I don’t want any of that. I simply wish to
say what I tell you: Fair marchioness, your beautiful eyes make me
die of love.
Professor of Philosophy. Still, you might amplify the thing a little?
M. Jourdain. No, I tell you, I will have nothing but those very
words in the letter; but they must be put in a fashionable way, and
arranged as they should be. Pray explain a little, so that I may see
the different ways in which they can be put.
Professor of Philosophy. They may be put, first of all, as you have
said, Fair marchioness, your beautiful eyes make me die of love; or
else, Of love die make me, fair marchioness, your beautiful eyes; or,
Your beautiful eyes of love make me, fair marchioness, die; or, Die of
love your beautiful eyes, fair marchioness, make me; or else, Me
make your beautiful eyes die, fair marchioness, of love.
M. Jourdain. But of all these ways, which is the best?
Professor of Philosophy. The one you said—Fair marchioness,
your beautiful eyes make me die of love.
M. Jourdain. Yet I have never studied, and I did all that right off at
the first shot. I thank you with all my heart, and I beg you to come
early again to-morrow morning.
Professor of Philosophy.—I shall not fail you.

Paul Scarron, described as a “pure bird of pleasure,” wrote plays,


novels, epigrams, letters, and best known of all, a classic burlesque
called Virgile Travesti.
Quotations cannot be made from his longer works, but two poems
are given.

You might also like