SlideShare a Scribd company logo
iOS 5 Programming Cookbook Solutions Examples for
iPhone iPad and iPod touch Apps 1st Edition Vandad
Nahavandipoor download pdf
https://ebookfinal.com/download/ios-5-programming-cookbook-solutions-
examples-for-iphone-ipad-and-ipod-touch-apps-1st-edition-vandad-
nahavandipoor/
Visit ebookfinal.com today to download the complete set of
ebook or textbook!
Here are some recommended products that we believe you will be
interested in. You can click the link to download.
iOS 8 Swift Programming Cookbook Solutions Examples for
iOS Apps 1st Edition Nahavandipoor
https://ebookfinal.com/download/ios-8-swift-programming-cookbook-
solutions-examples-for-ios-apps-1st-edition-nahavandipoor/
Beginning iOS Apps with Facebook and Twitter APIs for
iPhone iPad and iPod touch 1st Edition Chris Dannen
https://ebookfinal.com/download/beginning-ios-apps-with-facebook-and-
twitter-apis-for-iphone-ipad-and-ipod-touch-1st-edition-chris-dannen/
Programming iOS 4 Fundamentals of iPhone iPad and iPod
Touch Development 1st Edition Matt Neuburg
https://ebookfinal.com/download/programming-ios-4-fundamentals-of-
iphone-ipad-and-ipod-touch-development-1st-edition-matt-neuburg/
iOS 4 in Action Examples and Solutions for iPhone iPad
Jocelyn Harrington
https://ebookfinal.com/download/ios-4-in-action-examples-and-
solutions-for-iphone-ipad-jocelyn-harrington/
iPhone and iPad Apps Marketing Secrets to Selling Your
iPhone and iPad Apps Que Biz Tech 1st Edition Jeffrey
Hughes
https://ebookfinal.com/download/iphone-and-ipad-apps-marketing-
secrets-to-selling-your-iphone-and-ipad-apps-que-biz-tech-1st-edition-
jeffrey-hughes/
iPhone and iPad Apps for Absolute Beginners 1st Edition
Dr. Rory Lewis (Auth.)
https://ebookfinal.com/download/iphone-and-ipad-apps-for-absolute-
beginners-1st-edition-dr-rory-lewis-auth/
iOS Wow Factor UX Design Techniques for iPhone and iPad
1st Edition Tim Wood
https://ebookfinal.com/download/ios-wow-factor-ux-design-techniques-
for-iphone-and-ipad-1st-edition-tim-wood/
Bash Cookbook Solutions and Examples for bash Users 1st
Edition Albing
https://ebookfinal.com/download/bash-cookbook-solutions-and-examples-
for-bash-users-1st-edition-albing/
iPad and iPhone Kung Fu Tips Tricks Hints and Hacks for
iOS 7 1st Edition Keir Thomas
https://ebookfinal.com/download/ipad-and-iphone-kung-fu-tips-tricks-
hints-and-hacks-for-ios-7-1st-edition-keir-thomas/
iOS 5 Programming Cookbook Solutions Examples for iPhone iPad and iPod touch Apps 1st Edition Vandad Nahavandipoor
iOS 5 Programming Cookbook Solutions Examples for
iPhone iPad and iPod touch Apps 1st Edition Vandad
Nahavandipoor Digital Instant Download
Author(s): Vandad Nahavandipoor
ISBN(s): 9781449311438, 1449311431
Edition: 1
File Details: PDF, 24.94 MB
Year: 2012
Language: english
iOS 5 Programming Cookbook
Vandad Nahavandipoor
Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
iOS 5 Programming Cookbook
by Vandad Nahavandipoor
Revision History for the :
See http://oreilly.com/catalog/errata.csp?isbn=9781449311438 for release details.
ISBN: 978-1-449-31143-8
1318287906
To Agnieszka Marta Dybowska.
iOS 5 Programming Cookbook Solutions Examples for iPhone iPad and iPod touch Apps 1st Edition Vandad Nahavandipoor
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Creating a Simple iOS App in Xcode 2
1.2 Understanding Interface Builder 7
1.3 Compiling iOS Apps 13
1.4 Running iOS Apps on the Simulator 15
1.5 Running iOS Apps on iOS Devices 17
1.6 Packaging iOS Apps For Distribution 21
1.7 Declaring Variables in Objective-C 27
1.8 Comparing Values in Objective-C with an If Statement 29
1.9 Implementing Loops with For Statements 32
1.10 Implementing While Loops 35
1.11 Creating Custom Classes 37
1.12 Defining Functionality for Classes 42
1.13 Defining Two or More Methods with the Same Name 46
1.14 Allocating and Initializing Objects 49
1.15 Adding Properties to Classes 51
1.16 Moving from Manual Reference Counting to Automatic Reference
Counting 54
1.17 Typecasting with Automatic Reference Counting 59
1.18 Delegating Tasks with Protocols 63
1.19 Determining Whether Instance or Class Methods are Available 71
1.20 Determining Whether a Class is Available at Run Time 74
1.21 Allocating and Making Use of Strings 75
1.22 Allocating and Making Use of Numbers 79
1.23 Allocating and Making Use of Arrays 81
1.24 Allocating and Making Use of Dictionaries 86
1.25 Allocating and Making Use of Sets 89
1.26 Creating Bundles 92
1.27 Loading Data From the Main Bundle 93
1.28 Loading Data From Other Bundles 97
iii
1.29 Sending Notifications with NSNotificationCenter 100
1.30 Listening for Notifications Sent From NSNotificationCenter 104
2. Implementing Controllers and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
2.1 Displaying Alerts with UIAlertView 110
2.2 Creating and Using Switches with UISwitch 121
2.3 Picking Values with UIPickerView 126
2.4 Picking Date and Time with UIDatePicker 136
2.5 Implementing Range Pickers with UISlider 144
2.6 Grouping Compact Options with UISegmentedControl 151
2.7 Presenting and Managing Views with UIViewController 159
2.8 Implementing Navigation with UINavigationController 163
2.9 Manipulating a Navigation Controller’s Array of
View Controllers 170
2.10 Displaying an Image on a Navigation Bar 171
2.11 Adding Buttons to Navigation Bars with UIBarButtonItem 172
2.12 Presenting Multiple View Controllers with UITabBarController 182
2.13 Displaying Static Text with UILabel 191
2.14 Accepting User Text Input with UITextField 196
2.15 Displaying Long Lines of Text with UITextView 208
2.16 Adding Buttons to the User Interface with UIButton 214
2.17 Displaying Images with UIImageView 220
2.18 Creating Scrollable Content with UIScrollView 226
2.19 Loading Web Pages with UIWebView 231
2.20 Presenting Master-Detail Views with UISplitViewController 237
2.21 Enabling Paging with UIPageViewController 245
2.22 Displaying Popovers with UIPopoverController 251
2.23 Displaying Progress with UIProgressView 262
2.24 Listening and Reacting to Keyboard Notifications 264
3. Constructing and Using Table Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
3.1 Instantiating a Table View 281
3.2 Assigning a Delegate to a Table View 284
3.3 Populating a Table View with Data 286
3.4 Receiving and Handling Table View Events 290
3.5 Using Different Types of Accessories in a Table View Cell 291
3.6 Creating Custom Table View Cell Accessories 294
3.7 Displaying Hierarchical Data in Table Views 297
3.8 Enabling Swipe Deletion of Table View Cells 299
3.9 Constructing Headers and Footers in Table Views 302
3.10 Displaying Context Menus on Table Views Cells 314
3.11 Moving Cells and Sections in Table Views 319
3.12 Deleting Cells and Sections from Table Views 326
iv | Table of Contents
4. Storyboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
4.1 Creating a Project with Storyboards 330
4.2 Adding a Navigation Controller to a Storyboard 333
4.3 Passing Data From One Screen to Another 346
4.4 Adding a Storyboard to an Existing Project 349
5. Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
5.1 Constructing Block Objects 358
5.2 Accessing Variables in Block Objects 362
5.3 Invoking Block Objects 369
5.4 Dispatching Tasks to Grand Central Dispatch 370
5.5 Performing UI-Related Tasks with GCD 371
5.6 Performing Non-UI-Related Tasks Synchronously with GCD 376
5.7 Performing Non-UI Related Tasks Asynchronously with GCD 379
5.8 Performing Tasks After a Delay with GCD 385
5.9 Performing a Task at Most Once with GCD 388
5.10 Grouping Tasks Together with GCD 390
5.11 Constructing Your Own Dispatch Queues with GCD 394
5.12 Running Tasks Synchronously with Operations 397
5.13 Running Tasks Asynchronously with Operations 404
5.14 Creating Dependency Between Operations 411
5.15 Creating Concurrency with Timers 413
5.16 Creating Concurrency with Threads 418
5.17 Invoking Background Methods 424
5.18 Exiting Threads and Timers 425
6. Core Location and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
6.1 Creating a Map View 431
6.2 Handling the Events of a Map View 435
6.3 Pinpointing the Location of a Device 436
6.4 Displaying Pins on a Map View 439
6.5 Displaying Pins with Different Colors on a Map View 443
6.6 Displaying Custom Pins on a Map View 449
6.7 Converting Longitude and Latitude to a Meaningful Address 452
6.8 Converting Meaningful Addresses to Longitude and Latitude 455
7. Implementing Gesture Recognizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
7.1 Detecting Swipe Gestures 459
7.2 Detecting Rotation Gestures 461
7.3 Detecting Panning and Dragging Gestures 465
7.4 Detecting Long Press Gestures 467
7.5 Detecting Tap Gestures 470
7.6 Detecting Pinch Gestures 472
Table of Contents | v
8. Networking, JSON, XML and Twitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
8.1 Downloading Asynchronously with NSURLConnection 475
8.2 Handling Timeouts in Asynchronous Connections 478
8.3 Downloading Synchronously with NSURLConnection 479
8.4 Modifying a URL Request with NSMutableURLRequest 482
8.5 Sending HTTP GET Requests with NSURLConnection 483
8.6 Sending HTTP POST Requests with NSURLConnection 486
8.7 Sending HTTP DELETE Requests with NSURLConnection 489
8.8 Sending HTTP PUT Requests with NSURLConnection 491
8.9 Serializing Arrays and Dictionaries into JSON 494
8.10 Deserializing JSON into Arrays and Dictionaries 496
8.11 Integrating Twitter Functionality Into Your Apps 499
8.12 Parsing XML with NSXMLParser 508
9. Audio and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
9.1 Playing Audio Files 515
9.2 Handling Interruptions While Playing Audio 517
9.3 Recording Audio 518
9.4 Handling Interruptions While Recording Audio 525
9.5 Playing Audio over Other Active Sounds 526
9.6 Playing Video Files 530
9.7 Capturing Thumbnails from a Video File 534
9.8 Accessing the Music Library 537
10. Address Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
10.1 Getting a Reference to Address Book 550
10.2 Retrieving All the People in the Address Book 552
10.3 Retrieving Properties of Address Book Entries 553
10.4 Inserting a Person Entry into the Address Book 558
10.5 Inserting a Group Entry into the Address Book 562
10.6 Adding Persons to Groups 565
10.7 Searching the Address Book 569
10.8 Retrieving and Setting a Person’s Address Book Image 573
11. Camera and the Photo Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
11.1 Detecting and Probing the Camera 583
11.2 Taking Photos with the Camera 588
11.3 Taking Videos with the Camera 592
11.4 Storing Photos in the Photo Library 595
11.5 Storing Videos in the Photo Library 598
11.6 Retrieving Photos and Videos from the Photo Library 600
11.7 Retrieving Assets from the Assets Library 601
11.8 Editing Videos on an iOS Device 609
vi | Table of Contents
12. Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
12.1 Detecting the Availability of Multitasking 616
12.2 Completing a Long-Running Task in the Background 617
12.3 Receiving Local Notifications in the Background 621
12.4 Playing Audio in the Background 630
12.5 Handling Location Changes in the Background 633
12.6 Saving and Loading the State of Multitasking iOS Apps 636
12.7 Handling Network Connections in the Background 639
12.8 Handling Notifications Delivered to a Waking App 642
12.9 Responding to Changes in App Settings 644
12.10 Opting Out of Multitasking 647
13. Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
13.1 Creating a Core Data Model with Xcode 653
13.2 Generating Class Files for Core Data Entities 658
13.3 Creating and Saving Data Using Core Data 661
13.4 Reading Data from Core Data 663
13.5 Deleting Data From Core Data 666
13.6 Sorting Data in Core Data 669
13.7 Boosting Data Access in Table Views 671
13.8 Implementing Relationships in Core Data 683
14. Dates, Calendars and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691
14.1 Retrieving the List of Calendars 694
14.2 Adding Events to Calendars 696
14.3 Accessing the Contents of Calendars 700
14.4 Removing Events from Calendars 705
14.5 Adding Recurring Events to Calendars 709
14.6 Retrieving the Attendees of an Event 714
14.7 Adding Alarms to Calendars 719
14.8 Handling Event Changed Notifications 721
14.9 Presenting Event View Controllers 724
14.10 Presenting Event Edit View Controllers 729
15. Graphics and Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
15.1 Enumerating and Loading Fonts 745
15.2 Drawing Text 747
15.3 Constructing, Setting, and Using Colors 749
15.4 Drawing Images 754
15.5 Drawing Lines 759
15.6 Constructing Paths 766
15.7 Drawing Rectangles 771
15.8 Adding Shadows to Shapes 776
Table of Contents | vii
15.9 Drawing Gradients 783
15.10 Displacing Shapes Drawn on Graphic Contexts 792
15.11 Scaling Shapes Drawn on Graphic Contexts 796
15.12 Rotating Shapes Drawn on Graphic Contexts 800
15.13 Animating and Moving Views 802
15.14 Animating and Scaling Views 812
15.15 Animating and Rotating Views 814
16. Core Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
16.1 Detecting the Availability of an Accelerometer 819
16.2 Detecting the Availability of a Gyroscope 821
16.3 Retrieving Accelerometer Data 822
16.4 Detecting Shakes on an iOS Device 825
16.5 Retrieving Gyroscope Data 830
17. iCloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833
17.1 Setting Up Your App For iCloud 834
17.2 Storing and Synchronizing Dictionaries in iCloud 840
17.3 Creating and Managing Folders for Apps in iCloud 845
17.4 Searching for Files and Folders in iCloud 852
17.5 Storing User Documents in iCloud 862
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
viii | Table of Contents
Preface
I started developing iPhone applications in late 2007. Between then and now, I have
worked on various iPhone OS applications for different companies across the globe.
As you might have already guessed, iOS is my favorite platform and Objective-C is my
favorite programming language. I find that Objective-C helps programmers write clean
code and iOS helps developers write user-friendly and useful applications.
I have coded in other programming languages such as Assembly (using NASM and
TASM) and Delphi/Pascal for many years, and I still find myself going through disas-
sembled Objective-C code to find out which method of doing a certain thing or
accomplishing a certain result in Objective-C is better optimized on a certain device
and/or operating system.
After becoming comfortable with the iOS SDK, I gradually built up a thirst to write a
book about the iOS SDK, and with the help of wonderful people at O’Reilly, you are
now reading the result of the several hundred hours that have been put into writing
new material for iOS 3 and iOS 4, editing, reviewing, revising, and publishing.
So, please go ahead and start exploring the recipes. I hope you’ll find that they are easy
to cook and digest!
Audience
I assume you are comfortable with the iOS development environment and know how
to create an app for the iPhone or iPad. This book does not get novice programmers
started, but presents useful ways to get things done for iOS programmers ranging from
novices to experts.
Organization of This Book
In this book, we will discuss frameworks and classes that are available in iOS 3 and iOS
4. In some recipes, you will find code that runs only on iOS 4 and later; in those recipes,
I note that you will need the iOS 4 SDK or later to compile the example code.
ix
Here is a concise breakdown of the material each chapter covers:
Chapter 1, The Basics
Explains how Objective-C classes are structured and how objects can be instanti-
ated. The chapter talks about properties and delegates as well as memory man-
agement in Objective-C. Even if you are competent in Objective-C, I strongly
suggest that you go through this chapter, even if you are skimming through it, to
understand the basic material that is used in the rest of the chapters.
Chapter 2, Implementing Controllers and Views
Describes various approaches to constructing your iOS application’s user interface
by taking advantage of different tools the SDK provides. This chapter also intro-
duces you to features that are only available on the iPad, such as the popover and
split view controllers.
Chapter 3, Constructing and Using Table Views
Shows how you can work with table views to create professional-looking iOS
applications. Table views are very dynamic in nature, and as a result, programmers
sometimes have difficulty understanding how they should work with them. By
reading this chapter and having a look at and trying out the example code, you will
gain the knowledge that is required to comfortably work with table views.
Chapter 6, Core Location and Maps
Describes how you should use Map Kit and Core Location APIs to develop
location-aware iOS applications. First you will learn about maps, and then you will
learn how to detect a device’s location and tailor your maps with custom annota-
tions. You will also learn about geocoding and reverse geocoding, as well as some
of the methods of the Core Location framework, which are only available in the
iOS 4 SDK and later.
Chapter 7, Implementing Gesture Recognizers
Demonstrates how to use gesture recognizers, which enable your users to easily
and intuitively manipulate the graphical interface of your iOS applications. In this
chapter, you will learn how to use all available gesture recognizers in the iOS SDK,
with working examples tested on iOS 3 and iOS 4 on different devices such as the
iPhone 3GS, iPhone 4, and iPad.
Chapter 8, Networking, JSON, XML and Twitter
Demonstrates how to download data from a URL and parse XML files. You will
learn about synchronous and asynchronous connections and their pros and cons.
You will also learn about caching files in memory and on disk to avoid consuming
the possibly limited bandwidth of an iOS device on which your application could
be running.
Chapter 9, Audio and Video
Discusses the AV Foundation and Media Player frameworks that are available on
the iOS SDK. You will learn how to play audio and video files and how to handle
interruptions, such as a phone call, while the audio or video is being played on
x | Preface
both iOS 3 and iOS 4. This chapter also explains how to record audio using an iOS
device’s built-in microphone(s). At the end of the chapter, you will learn how to
access the iPod Library and play its media content, all from inside your application.
Chapter 10, Address Book
Explains the Address Book framework and how to retrieve contacts, groups, and
their information from the Address Book database on an iOS device. The Address
Book framework is composed entirely of C APIs. Because of this, many Objective-
C developers find it difficult to use this framework compared to frameworks that
provide an Objective-C interface. After reading this chapter and trying the exam-
ples for yourself, you will feel much more confident using the Address Book
framework.
Chapter 11, Camera and the Photo Library
Demonstrates how you can determine the availability of front- and back-facing
cameras on an iOS device. Some of the recipes in this chapter are specific to iOS
4, with the rest working on both iOS 3 and iOS 4. You will also learn how to access
the Photo Library using the Assets Library framework which is available in iOS 4
and later. At the end of the chapter, you will learn about editing videos right on an
iOS device using a built-in view controller.
Chapter 12, Multitasking
Explains, with examples, how to create multitasking-aware applications that run
beautifully on iOS 4. You will learn about background processing, from playing
audio and retrieving users’ locations in the background, to downloading content
from a URL while your application is running in the background.
Chapter 13, Core Data
Describes how to maintain persistent storage for your iOS applications using Core
Data. You will learn how to add to, delete from, and edit Core Data objects and
how to boost access to data in a table view. In addition, you will learn how to
manage relationships between Core Data objects.
Chapter 14, Dates, Calendars and Events
Demonstrates the use of the Event Kit and Event Kit UI frameworks, which are
available on iOS 4 and later, in order to manage calendars and events on an iOS
device. You will see how to create, modify, save, and delete events. You will also
learn, through examples, how to add alarms to calendar events and how to set up
CalDAV calendars so that you can share a single calendar among multiple devices.
Chapter 15, Graphics and Animations
Introduces the Core Graphics framework. You will learn how to draw images and
text on a graphics context, grab the contents of a graphics context and save it as
an image, and much more.
Chapter 16, Core Motion
Explains the Core Motion framework, which is new in iOS 4. Using Core Motion,
you will access the accelerometer and the gyroscope on an iOS device. You will
also learn how to detect shakes on a device. Of course, not all iOS devices are
Preface | xi
equipped with an accelerometer and a gyroscope, so you will also learn how to
detect the availability of the required hardware.
Additional Resources
Fromtimetotime,IrefertoofficialAppledocumentation.SomeofApple’sdescriptions
are right on the mark, and there is no point in trying to restate them. Throughout this
book, I have listed the most important documents and guides in the official Apple
documentation that every professional iOS developer should read.
For starters, I suggest that you have a look at the “iPhone Human Interface Guidelines”
and the “iPad Human Interface Guidelines.” These two documents will tell you every-
thing you should know about developing engaging and intuitive user interfaces for the
iPhone/iPod and the iPad. Every iOS programmer must read these documents. In fact,
I believe these documents must be read by the product design and development teams
of any company that develops iOS applications.
iPhone Human Interface Guidelines
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/
mobilehig/Introduction/Introduction.html
iPad Human Interface Guidelines
https://developer.apple.com/library/ios/#documentation/General/Conceptual/iPad
HIG/Introduction/Introduction.html
I also suggest that you skim through the “iOS Application Programming Guide” in the
iOS Reference Library for some tips and advice on how to make great iOS applications:
https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPho
neOSProgrammingGuide/Introduction/Introduction.html
One of the things you will notice when reading Chapter 12 is the use of block objects.
This book concisely explains block objects, but if you require further details on the
subject, I suggest you read “A Short Practical Guide to Blocks,” available at this URL:
https://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide
_Blocks/index.html%23/apple_ref/doc/uid/TP40009758
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyVa
lueCoding/KeyValueCoding.html
Throughout this book, you will see references to “bundles” and loading images and
data from bundles. You will read a concise overview about bundles in this book, but if
you require further information, head over to the “Bundle Programming Guide,” avail-
able at this URL:
https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptu
al/CFBundles/Introduction/Introduction.html
xii | Preface
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, filenames, file extensions, and directories
Constant width
Indicates variables and other code elements, the contents of files, and the output
from commands
Constant width bold
Highlights text in examples that is new or particularly significant in a recipe
Constant width italic
Shows text that should be replaced with user-supplied values
This icon signifies a tip, suggestion, or general note.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in
this book in your programs and documentation. You do not need to contact us for
permission unless you’re reproducing a significant portion of the code. For example,
writing a program that uses several chunks of code from this book does not require
permission. Selling or distributing a CD-ROM of examples from O’Reilly books does
require permission. Answering a question by citing this book and quoting example
code does not require permission. Incorporating a significant amount of example code
from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the
title, author, publisher, and ISBN. For example: “iOS 4 Programming Cookbook, by
Vandad Nahavandipoor (O’Reilly). Copyright 2011 Vandad Nahavandipoor, 978-1-
449-38822-5.”
If you feel your use of code examples falls outside fair use or the permission given here,
feel free to contact us at permissions@oreilly.com.
We’d Like to Hear from You
Every example and code snippet in this book has been tested on the iPhone 4, iPad,
iPhone 3GS, and iPhone/iPad Simulator, but occasionally you may encounter
problems—for example, if you have a different version of the SDK than the version on
which the example code was compiled and tested. The information in this book has
Preface | xiii
also been verified at each step of the production process. However, mistakes and over-
sights can occur, and we will gratefully receive details of any you find, as well as any
suggestions you would like to make for future editions. You can contact the author and
editors at:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international or local)
(707) 829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at:
http://oreilly.com/catalog/9781449388225
There is also a companion website to this book where you can see all the examples with
color-highlighted syntax:
http://www.ios4cookbook.com
To comment or ask technical questions about this book, send email to the following
address, mentioning the book’s ISBN number (9781449388225):
bookquestions@oreilly.com
For more information about our books, conferences, Resource Centers, and the
O’Reilly Network, see our website at:
http://www.oreilly.com
Safari® Books Online
Safari Books Online is an on-demand digital library that lets you easily
search more than 7,500 technology and creative reference books and vid-
eos to find the answers you need quickly.
Withasubscription,youcanreadanypageandwatchanyvideofromourlibraryonline.
Read books on your cell phone and mobile devices. Access new titles before they are
available for print, and get exclusive access to manuscripts in development and post
feedback for the authors. Copy and paste code samples, organize your favorites, down-
load chapters, bookmark key sections, create notes, print out pages, and benefit from
tons of other time-saving features.
O’Reilly Media has uploaded this book to the Safari Books Online service. To have full
digital access to this book and others on similar topics from O’Reilly and other pub-
lishers, sign up for free at http://my.safaribooksonline.com.
xiv | Preface
Acknowledgments
I have always loved writing and running my programs on computers. I look at pro-
gramming as a way to speak to whatever computer the program runs on. To me,
programming is a way to actually connect with the computer and give it instructions
and listen to what it says in return.
I have been exceptionally lucky to have almost always found the right people to help
me find the right path in whatever journey I’ve started in my life. First and foremost, I
would like to thank my beloved fiancée, Agnieszka Marta Dybowska, for her uncon-
ditional love and support throughout the years and for the many hours she had to
spendwithoutmewhileIwasworkingonthisbook.Your strongenthusiasmforwriting
and journalism has greatly moved me, and I sincerely hope that one day you will gather
enough material to be able to write your book.
I also want to thank Brian Jepson (whose many hidden talents are yet to be discovered!)
for giving me a chance to work on this book. This book would have been impossible
if it wasn’t for Brian’s consistent urge to improve the outline and the table of contents
that I originally sent him. This reminds me to thank Andy Oram, whom I would like
to call the virtual second writer of this book. Andy’s perfectionism and his undeniable
desire to finely form every single sentence you read in this book are absolutely impres-
sive. I must also thank Sarah Kim and Rachel James for helping me update my profile
page on O’Reilly’s website. I also appreciate Meghan Blanchette’s help in doing the
initial paperwork for this book.
I want to say a big thank you to my technical reviewers, Eric Blair and Alasdair Allan,
for all their helpful insight. Kirk Pattinson, Gary McCarville, and Sushil Shirke are
among the people who have greatly influenced me to become who I am today. Thank
you to Sushil for being a great mentor and colleague and for providing continuous
support. Thanks to Kirk for believing that I was up to the challenge of working on some
high-profile iOS applications. Thank you to Gary for his support while I worked on
this project, and for being a wonderful mentor.
Last but not least, I would like to sincerely thank Apple and its employees for making
such a wonderful operating system and SDK. It’s truly a great pleasure to work with
the iOS SDK, and I hope you, the reader, will enjoy working with it as much as I do.
Preface | xv
iOS 5 Programming Cookbook Solutions Examples for iPhone iPad and iPod touch Apps 1st Edition Vandad Nahavandipoor
CHAPTER 1
The Basics
1.0 Introduction
A lot has changed in iPhone, iPad, and iPod Touch programming since the introduction
of iOS 5. The whole runtime and the way we write Objective-C code has dramatically
changed. ARC (Automatic Reference Counting) is now introduced into the LLVM
Compiler, which in some ways gives us more flexibility and in other ways makes the
runtime more fragile. In this chapter, we will get down and dirty with objects and how
we can use them using the modern Objective-C runtime under ARC.
Objects are defined according to classes, and therefore these two terms are commonly
used interchangeably. But actually, a class is just a specification for defining objects;
each object is said to be an instance of its class. Each class—and therefore the objects
that are created from that class—is a set of properties, tasks, methods, enumerations,
and much more. In an object-oriented programming language, classes can inherit from
each other much like a person can inherit certain traits and characteristics from his
parents.
Objective-C does not allow multiple inheritance. Therefore, every class
is the direct descendant of, at most, one other class.
The root class of most Objective-C objects is the NSObject class. This class manages the
runtime capabilities offered by iOS; as a result, any class that directly or indirectly
inherits from NSObject will inherit these capabilities as well. As we will see later in this
chapter, objects that inherit from NSObject can take advantage of Objective-C’s dis-
tinctive memory management model.
1
1.1 Creating a Simple iOS App in Xcode
Problem
You've started to learn iOS Programming and you want to create a really simple iOS
Project and app in Xcode.
Solution
Create a new iOS Project in Xcode and then run it in the iOS Simulator using Command
+Shift+R.
Discussion
I'll assume you have a Mac and you have already installed the Xcode set of tools. Now
you want to create an iOS Project and run that App on the iOS Simulator. This process
is really straightforward:
1. Open Xcode if you don't have it open yet.
2. Select File on the menu bar, select New, and then select New Project. You will be
greeted with a screen similar to that shown in Figure 1-1.
Figure 1-1. The New Project dialog in Xcode
2 | Chapter 1: The Basics
Random documents with unrelated
content Scribd suggests to you:
Grove’s Cell, 1838.
This consisted of zinc, sulphuric acid, nitric
acid and platinum. It made a very powerful
battery. The nitric acid is called the depolarizer
as it absorbs the hydrogen gas formed, thus
improving the operating voltage.
GROVE’S BATTERY
Sir William Robert Grove, an English Judge and scientist,
invented a cell in 1838 consisting of a platinum electrode in strong
nitric acid in a porous earthenware jar. This jar was put in dilute
sulphuric acid in a glass jar in which there was an amalgamated zinc
plate for the other electrode. This had an open circuit voltage of
about 1.9 volts. The porous jar was used to prevent the nitric acid
from attacking the zinc. The nitric acid was used for the purpose of
combining with the hydrogen gas set free by the action of the
sulphuric acid on the zinc, and hence was the depolarizing agent.
Hydrogen combining with nitric acid forms nitrous peroxide and
water. Part of the nitrous peroxide is dissolved in the water, and the
rest escapes as fumes which, however, are very suffocating.
The chemical equations of this cell are as follows:
In outer glass jar: Zn + H2SO4 = ZnSO4 + H2
In inner porous jar: H2 + 2HNO3 = N2O4 + 2H2O
An interesting thing about Grove’s cell is that it was planned in
accordance with a theory. Grove knew that the electrical energy of
the zinc-sulphuric acid cell came from the chemical affinity of the two
reagents, and if the hydrogen gas set free could be combined with
oxygen (to form water—H2O), such chemical affinity should increase
the strength of the cell. As the hydrogen gas appears at the other
electrode, the oxidizing agent should surround that electrode. Nitric
acid was known at that time as one of the most powerful oxidizing
liquids, but as it attacks copper, he used platinum for the other
electrode. Thus he not only overcame the difficulty of polarization by
the hydrogen gas, but also increased the voltage of the cell by the
added chemical action of the combination of hydrogen and oxygen.
GROVE’S DEMONSTRATION OF
INCANDESCENT LIGHTING
In 1840 Grove made an experimental lamp by attaching the ends
of a coil of platinum wire to copper wires, the lower parts of which
were well varnished for insulation. The platinum wire was covered by
a glass tumbler, the open end set in a glass dish partly filled with
water. This prevented draughts of air from cooling the incandescent
platinum, and the small amount of oxygen of the air in the tumbler
reduced the amount of oxidization of the platinum that would
otherwise occur. With current supplied by a large number of cells of
his battery, he lighted the auditorium of the Royal Institution with
these lamps during one of the lectures he gave. This lamp gave only
a feeble light as there was danger of melting the platinum and
platinum gives but little light unless operated close to its melting
temperature. It also required a lot of current to operate it as the air
tended to cool the incandescent platinum. The demonstration was
only of scientific interest, the cost of current being much too great
(estimated at several hundred dollars a kilowatt hour) to make it
commercial.
GRENET BATTERY
It was discovered that chromic anhydride gives up oxygen easier
than nitric acid and consequently if used would give a higher voltage
than Grove’s nitric acid battery. It also has the advantage of a lesser
tendency to attack zinc directly if it happens to come in contact with
it. Grenet developed a cell having a liquid consisting of a mixture of
potassium bichromate (K2Cr2O7) and sulphuric acid. A porous cell
was therefore not used to keep the two liquids apart. This had the
advantage of reducing the internal resistance. The chemical reaction
was:
K_{2}Cr_{2}O_{7} (potassium bichromate) + 7H_{2}SO_{4}
(sulphuric acid) + 3Zn (zinc) = 3ZnSO4 (zinc sulphate) +
K2SO4 (potassium sulphate) + Cr2 (SO4)3 (chromium
sulphate) + 7H2O (water).
In order to prevent the useless consumption of zinc on open
circuit, the zinc was attached to a sliding rod and could be drawn up
into the neck of the bottle-shaped jar containing the liquid.
Grove’s Incandescent Lamp, 1840.
Grove made an experimental lamp, using
platinum for the burner which was protected
from draughts of air by a glass tumbler.
DE MOLEYNS’ INCANDESCENT
LAMP
Frederick De Moleyns, an Englishman, has the honor of having
obtained the first patent on an incandescent lamp. This was in 1841
and his lamp was quite novel. It consisted of a spherical glass globe,
in the upper part of which was a tube containing powdered charcoal.
This tube was open at the bottom inside the globe and through it ran
a platinum wire, the end below the tube being coiled. Another
platinum wire coiled at its upper end came up through the lower part
of the globe but did not quite touch the other platinum coil. The
powdered charcoal filled the two coils of platinum wire and bridged
the gap between. Current passing through this charcoal bridge
heated it to incandescence. The air in the globe having been
removed as far as was possible with the hand air pumps then
available, the charcoal did not immediately burn up, the small
amount consumed being replaced by the supply in the tube. The
idea was ingenious but the lamp was impractical as the globe rapidly
blackened from the evaporation of the incandescent charcoal.
De Moleyns’ Incandescent Lamp, 1841.
This consisted of two coils of platinum wire
containing powdered charcoal operating in a
vacuum. It is only of interest as the first
incandescent lamp on which a patent (British)
was granted.
EARLY DEVELOPMENTS OF THE
ARC LAMP
It had been found that most of the light of the arc came from the
tip of the positive electrode, and that the charcoal electrodes were
rapidly consumed, the positive electrode about twice as fast as the
negative. Mechanisms were designed to take care of this, together
with devices to start the arc by allowing the electrodes to touch each
other and then pulling them apart the proper distance. This distance
varied from one-eighth to three-quarters of an inch.
In 1840 Bunsen, the German chemist who invented the bunsen
burner, devised a process for making hard dense carbon pencils
which lasted much longer than the charcoal previously used. The
dense carbon from the inside of the retorts of gas making plants was
ground up and mixed with molasses, moulded into shape and baked
at a high temperature. Bunsen also, in 1843, cheapened Grove’s
battery by substituting a hard carbon plate in place of the platinum
electrode.
Wright’s Arc Lamp, 1845.
This lamp is also only of interest as the first arc
lamp on which a patent (British) was granted.
Four arcs played between the five carbon discs.
Thomas Wright, an Englishman, was the first to patent an arc
lamp. This was in 1845, and the lamp was a hand regulated device
consisting of five carbon disks normally touching each other and
rotated by clockwork. Two of the disks could be drawn outward by
thumb screws, which was to be done after the current was turned on
thus establishing four arcs, one between each pair of disks. The next
year, 1846, W. E. Staite, another Englishman, made an arc lamp
having two vertical carbon pencils. The upper was stationary. The
lower was movable and actuated by clockwork directed by ratchets
which in turn were regulated by an electro-magnet controlled by the
current flowing through the arc. Thus the lower carbon would be
moved up or down as required.
Archereau, a Frenchman, made a very simple arc lamp in 1848.
The upper carbon was fixed and the lower one was mounted on a
piece of iron which could be drawn down into a coil of wire. The
weight of the lower electrode was overbalanced by a counterweight,
so that when no current was flowing the two carbons would touch.
When current was turned on, it flowed through the two carbons and
through the coil of wire (solenoid) which then became energized and
pulled the lower carbon down, thus striking the arc. Two of these arc
lamps were installed in Paris and caused considerable excitement.
After a few weeks of unreliable operation, it was found that the cost
of current from the batteries was much too great to continue their
use commercially. The dynamo had not progressed far enough to
permit its use.
Archereau’s Arc Lamp, 1848.
This simple arc was controlled by an electro-
magnet, and two lamps were installed for street
lighting in Paris, current being obtained from
batteries.
JOULE’S LAW
Joule was an Englishman, and in 1842 began investigating the
relation between mechanical energy and heat. He first showed that,
by allowing a weight to drop from a considerable height and turn a
paddle wheel in water, the temperature of the water would increase
in relation to the work done in turning the wheel. It is now known that
778 foot-pounds (1 lb. falling 778 feet, 10 lbs. falling 77.8 feet or 778
lbs. falling one foot, etc.) is the mechanical equivalent of energy
equal to raising one pound of water one degree Fahrenheit. The rate
of energy (power) is the energy divided by a unit of time; thus one
horsepower is 33,000 foot-pounds per minute. Joule next
investigated the relation between heat and electric current. He made
a device consisting of a vessel of water in which there were a
thermometer and an insulated coil of wire having a considerable
resistance. He found that an electric current heated the water, and
making many combinations of the amount and length of time of
current flowing and of the resistance of the wire, he deduced the law
that the energy in an electric circuit is proportional to the square of
the amount of current flowing multiplied by the length of time and
multiplied by the resistance of the wire.
The rate of electrical energy (electric power) is therefore
proportional to the square of current multiplied by the resistance. The
electrical unit of power is now called the Watt, named in honor of
James Watt, the Englishman, who made great improvements to the
steam engine about a century ago. Thus, watts = C2R and
substituting the value of R from Ohm’s law, C = E/R, we get
Watts = Volts × Amperes
The watt is a small unit of electric power, as can be seen from
the fact that 746 watts are equal to one horsepower. The kilowatt,
kilo being the Greek word for thousand, is 1000 watts.
This term is an important one in the electrical industry. For
example, dynamos are rated in kilowatts, expressed as KW; the
largest one made so far is 50,000 KW which is 66,666 horsepower.
Edison’s first commercial dynamo had a capacity of 6 KW although
the terms watts and kilowatts were not in use at that time. The
ordinary sizes of incandescent lamps now used in the home are 25,
40 and 50 watts.
STARR’S INCANDESCENT LAMP
Starr’s Incandescent Lamp, 1845.
This consisted of a short carbon pencil
operating in the vacuum above a column of
mercury.
J. W. Starr, an American, of Cincinnati, Ohio, assisted financially
by Peabody, the philanthropist, went to England where he obtained a
patent in 1845 on the lamps he had invented, although the patent
was taken out under the name of King, his attorney. One is of
passing interest only. It consisted of a strip of platinum, the active
length of which could be adjusted to fit the battery strength used, and
was covered by a glass globe to protect it from draughts of air. The
other, a carbon lamp, was the first real contribution to the art. It
consisted of a rod of carbon operating in the vacuum above a
column of mercury (Torrecellium vacuum) as in a barometer. A heavy
platinum wire was sealed in the upper closed end of a large glass
tube, and connected to the carbon rod by an iron clamp. The lower
end of the carbon rod was fastened to another iron clamp, the two
clamps being held in place and insulated from each other by a
porcelain rod. Attached to the lower clamp was a long copper wire.
Just below the lower clamp, the glass tube was narrowed down and
had a length of more than 30 inches. The tube was then filled with
mercury, the bottom of the tube being put into a vessel partly full of
mercury. The mercury ran out of the enlarged upper part of the tube,
coming to rest in the narrow part of the tube as in a barometer, so
that the carbon rod was then in a vacuum. One lamp terminal was
the platinum wire extending through the top of the tube, and the
other was the mercury. Several of these lamps were put on
exhibition in London, but were not a commercial success as they
blackened very rapidly. Starr started his return trip to the United
States the next year, but died on board the ship when he was but 25
years old.
OTHER EARLY INCANDESCENT
LAMPS
Staite’s Incandescent Lamp, 1848.
The burner was of platinum and iridium.
Roberts’ Incandescent Lamp, 1852.
It had a graphite burner operating in vacuum.
In 1848 W. E. Staite, who two years previously had made an arc
lamp, invented an incandescent lamp. This consisted of a platinum-
iridium burner in the shape of an inverted U, covered by a glass
globe. It had a thumb screw for a switch, the whole device being
mounted on a bracket which was used for the return wire. E. C.
Shepard, another Englishman, obtained a patent two years later on
an incandescent lamp consisting of a weighted hollow charcoal
cylinder the end of which pressed against a charcoal cone. Current
passing through this high resistance contact, heated the charcoal to
incandescence. It operated in a glass globe from which the air could
be exhausted. M. J. Roberts obtained an English patent in 1852 on
an incandescent lamp. This had a graphite rod for a burner, which
could be renewed, mounted in a glass globe. The globe was
cemented to a metallic cap fastened to a piece of pipe through which
the air could be exhausted. After being exhausted, the pipe, having a
stop cock, could be screwed on a stand to support the lamp.
Moses G. Farmer, a professor at the Naval Training Station at
Newport, Rhode Island, lighted the parlor of his home at 11 Pearl
Street, Salem, Mass., during July, 1859, with several incandescent
lamps having a strip of platinum for the burner. The novel feature of
this lamp was that the platinum strip was narrower at the terminals
than in the center. Heat is conducted away from the terminals and by
making the burner thin at these points, the greater resistance of the
ends of the burner absorbed more electrical energy thus offsetting
the heat being conducted away. This made a more uniform degree of
incandescence throughout the length of the burner, and Prof. Farmer
obtained a patent on this principle many years later (1882).
Farmer’s Incandescent Lamp, 1859.
This experimental platinum lamp was made by
Professor Farmer and several of them lighted
the parlor of his home in Salem, Mass.
FURTHER ARC LAMP
DEVELOPMENTS
During the ten years, 1850 to 1860, several inventors developed
arc lamp mechanisms. Among them was M. J. Roberts, who had
invented the graphite incandescent lamp. In Roberts’ arc lamp,
which he patented in 1852, the lower carbon was stationary. The
upper carbon fitted snugly into an iron tube. In the tube was a brass
covered iron rod, which by its weight could push the upper carbon
down the tube so the two carbons normally were in contact. An
electro-magnet in series with the arc was so located that, when
energized, it pulled up the iron tube. This magnet also held the brass
covered iron rod from pushing the upper carbon down the tube so
that the two carbons were pulled apart, striking the arc. When the arc
went out, the iron tube dropped back into its original position, the
brass covered iron rod was released, pushing the upper carbon
down the tube until the two carbons again touched. This closed the
circuit again, striking the arc as before.
Roberts’ Arc Lamp, 1852.
The arc was controlled by an electro-magnet
which held an iron tube to which the upper
carbon was fastened.
Slater and Watson’s Arc Lamp, 1852.
Clutches were used for the first time in this arc
lamp to feed the carbons.
In the same year (1852) Slater and Watson obtained an English
patent on an arc lamp in which the upper carbon was movable and
held in place by two clutches actuated by electro-magnets. The
lower carbon was fixed, and normally the two carbons touched each
other. When current was turned on, the electro-magnet lifted the
clutches which gripped the upper carbon, pulling it up and striking
the arc. This was the first time that a clutch was used to allow the
carbon to feed as it became consumed.
Henry Chapman, in 1855, made an arc in which the upper
carbon was allowed to feed by gravity, but held in place by a chain
wound around a wheel. On this wheel was a brake actuated by an
electro-magnet. The lower carbon was pulled down by an electro-
magnet working against a spring. When no current was flowing or
when the arc went out, the two carbons touched. With current on,
one electro-magnet set the brake and held the upper carbon
stationary. The other electro-magnet pulled the lower carbon down,
thus striking the arc.
None of these mechanisms regulated the length of the arc. It
was not until 1856 that Joseph Lacassagne and Henry Thiers,
Frenchmen, invented the so-called “differential” method of control,
which made the carbons feed when the arc voltage, and hence
length, became too great. This principle was used in commercial arc
lamps several years afterward when they were operated on series
circuits, as it had the added advantage of preventing the feeding of
one arc lamp affecting another on the same circuit. This differential
control consists in principle of two electro-magnets, one in series
with, and opposing the pull of the other which is in shunt with the arc.
The series magnet pulls the carbons apart and strikes the arc. As the
arc increases in length, its voltage rises, thereby increasing the
current flowing through the shunt magnet. This increases the
strength of the shunt magnet and, when the arc becomes too long,
the strength of the shunt becomes greater than that of the series
magnet, thus making the carbons feed.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!
ebookfinal.com

More Related Content

Similar to iOS 5 Programming Cookbook Solutions Examples for iPhone iPad and iPod touch Apps 1st Edition Vandad Nahavandipoor (20)

PDF
Programming Ios 5 2nd Edition 2nd Early Release Draft Matt Neuburg
lindzpeccoo
 
PDF
Learning Swift 3 Early release 3rd Edition Jonathan Manning
pundaiarleny
 
PDF
Learning the iPhone SDK for JavaScript Programmers Create Native Apps with Ob...
matbarnargis59
 
PDF
iOS 7 in Action 1st Edition Brendan G. Lim
bambildurdle
 
PDF
iOS 7 in Action 1st Edition Brendan G. Lim
laczaididdle
 
PDF
Learning Swift Building Apps for OSX, iOS, and Beyond Jon Manning 2024 scribd...
lalikaukpono
 
PDF
Programming iOS 4 Fundamentals of iPhone iPad and iPod Touch Development 1st ...
assangkaoua
 
PDF
Learning Swift Building Apps for OSX, iOS, and Beyond Jon Manning
batyegrocez1
 
PDF
Learning Swift 3 Early release 3rd Edition Jonathan Manning
nieysaiotti
 
PDF
Programming Ios 4 Fundamentals Of Iphone Ipad And Ipod Touch Development 1st ...
jungieaugi
 
PDF
Programming iOS 14 11th Edition Matt Neuburg
mouinarefin
 
PDF
Cocoa and Objective C Up and Running Foundations of Mac iPhone and iPod touch...
quizaloan
 
PDF
Peachpit mastering xcode 4 develop and design sep 2011
Jose Erickson
 
PPTX
Basic iOS Training with SWIFT - Part 1
Manoj Ellappan
 
PDF
Ios 12 Programming Fundamentals With Swift Swift Xcode And Cocoa Basics 5th E...
joettealhadi
 
PDF
iOS 14 Programming Fundamentals with Swift Covers iOS 14 Xcode 12 and Swift 5...
zfhvtdavx3598
 
PPTX
Hello world ios v1
Teodoro Alonso
 
PDF
Programming iOS 14 11th Edition Matt Neuburg
abrewvanneb3
 
PDF
Xcode4 userguide Apple
Pragati Singh
 
PDF
Ios 12 App Development Essentials Learn To Develop Ios 12 Apps With Xcode 10 ...
kotakyimam93
 
Programming Ios 5 2nd Edition 2nd Early Release Draft Matt Neuburg
lindzpeccoo
 
Learning Swift 3 Early release 3rd Edition Jonathan Manning
pundaiarleny
 
Learning the iPhone SDK for JavaScript Programmers Create Native Apps with Ob...
matbarnargis59
 
iOS 7 in Action 1st Edition Brendan G. Lim
bambildurdle
 
iOS 7 in Action 1st Edition Brendan G. Lim
laczaididdle
 
Learning Swift Building Apps for OSX, iOS, and Beyond Jon Manning 2024 scribd...
lalikaukpono
 
Programming iOS 4 Fundamentals of iPhone iPad and iPod Touch Development 1st ...
assangkaoua
 
Learning Swift Building Apps for OSX, iOS, and Beyond Jon Manning
batyegrocez1
 
Learning Swift 3 Early release 3rd Edition Jonathan Manning
nieysaiotti
 
Programming Ios 4 Fundamentals Of Iphone Ipad And Ipod Touch Development 1st ...
jungieaugi
 
Programming iOS 14 11th Edition Matt Neuburg
mouinarefin
 
Cocoa and Objective C Up and Running Foundations of Mac iPhone and iPod touch...
quizaloan
 
Peachpit mastering xcode 4 develop and design sep 2011
Jose Erickson
 
Basic iOS Training with SWIFT - Part 1
Manoj Ellappan
 
Ios 12 Programming Fundamentals With Swift Swift Xcode And Cocoa Basics 5th E...
joettealhadi
 
iOS 14 Programming Fundamentals with Swift Covers iOS 14 Xcode 12 and Swift 5...
zfhvtdavx3598
 
Hello world ios v1
Teodoro Alonso
 
Programming iOS 14 11th Edition Matt Neuburg
abrewvanneb3
 
Xcode4 userguide Apple
Pragati Singh
 
Ios 12 App Development Essentials Learn To Develop Ios 12 Apps With Xcode 10 ...
kotakyimam93
 

Recently uploaded (20)

PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PPTX
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PPTX
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
Modern analytical techniques used to characterize organic compounds. Birbhum ...
AyanHossain
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PPTX
Latest Features in Odoo 18 - Odoo slides
Celine George
 
PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PDF
water conservation .pdf by Nandni Kumari XI C
Directorate of Education Delhi
 
PPT
digestive system for Pharm d I year HAP
rekhapositivity
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PPTX
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
PPTX
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
ENGLISH LEARNING ACTIVITY SHE W5Q1.pptxY
CHERIEANNAPRILSULIT1
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
Nutrition Month 2025 TARP.pptx presentation
FairyLouHernandezMej
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
Modern analytical techniques used to characterize organic compounds. Birbhum ...
AyanHossain
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
ANORECTAL MALFORMATIONS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
Latest Features in Odoo 18 - Odoo slides
Celine George
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
water conservation .pdf by Nandni Kumari XI C
Directorate of Education Delhi
 
digestive system for Pharm d I year HAP
rekhapositivity
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
Maternal and Child Tracking system & RCH portal
Ms Usha Vadhel
 
PPT on the Development of Education in the Victorian England
Beena E S
 
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
How to Configure Access Rights of Manufacturing Orders in Odoo 18 Manufacturing
Celine George
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
Ad

iOS 5 Programming Cookbook Solutions Examples for iPhone iPad and iPod touch Apps 1st Edition Vandad Nahavandipoor

  • 1. iOS 5 Programming Cookbook Solutions Examples for iPhone iPad and iPod touch Apps 1st Edition Vandad Nahavandipoor download pdf https://ebookfinal.com/download/ios-5-programming-cookbook-solutions- examples-for-iphone-ipad-and-ipod-touch-apps-1st-edition-vandad- nahavandipoor/ Visit ebookfinal.com today to download the complete set of ebook or textbook!
  • 2. Here are some recommended products that we believe you will be interested in. You can click the link to download. iOS 8 Swift Programming Cookbook Solutions Examples for iOS Apps 1st Edition Nahavandipoor https://ebookfinal.com/download/ios-8-swift-programming-cookbook- solutions-examples-for-ios-apps-1st-edition-nahavandipoor/ Beginning iOS Apps with Facebook and Twitter APIs for iPhone iPad and iPod touch 1st Edition Chris Dannen https://ebookfinal.com/download/beginning-ios-apps-with-facebook-and- twitter-apis-for-iphone-ipad-and-ipod-touch-1st-edition-chris-dannen/ Programming iOS 4 Fundamentals of iPhone iPad and iPod Touch Development 1st Edition Matt Neuburg https://ebookfinal.com/download/programming-ios-4-fundamentals-of- iphone-ipad-and-ipod-touch-development-1st-edition-matt-neuburg/ iOS 4 in Action Examples and Solutions for iPhone iPad Jocelyn Harrington https://ebookfinal.com/download/ios-4-in-action-examples-and- solutions-for-iphone-ipad-jocelyn-harrington/
  • 3. iPhone and iPad Apps Marketing Secrets to Selling Your iPhone and iPad Apps Que Biz Tech 1st Edition Jeffrey Hughes https://ebookfinal.com/download/iphone-and-ipad-apps-marketing- secrets-to-selling-your-iphone-and-ipad-apps-que-biz-tech-1st-edition- jeffrey-hughes/ iPhone and iPad Apps for Absolute Beginners 1st Edition Dr. Rory Lewis (Auth.) https://ebookfinal.com/download/iphone-and-ipad-apps-for-absolute- beginners-1st-edition-dr-rory-lewis-auth/ iOS Wow Factor UX Design Techniques for iPhone and iPad 1st Edition Tim Wood https://ebookfinal.com/download/ios-wow-factor-ux-design-techniques- for-iphone-and-ipad-1st-edition-tim-wood/ Bash Cookbook Solutions and Examples for bash Users 1st Edition Albing https://ebookfinal.com/download/bash-cookbook-solutions-and-examples- for-bash-users-1st-edition-albing/ iPad and iPhone Kung Fu Tips Tricks Hints and Hacks for iOS 7 1st Edition Keir Thomas https://ebookfinal.com/download/ipad-and-iphone-kung-fu-tips-tricks- hints-and-hacks-for-ios-7-1st-edition-keir-thomas/
  • 5. iOS 5 Programming Cookbook Solutions Examples for iPhone iPad and iPod touch Apps 1st Edition Vandad Nahavandipoor Digital Instant Download Author(s): Vandad Nahavandipoor ISBN(s): 9781449311438, 1449311431 Edition: 1 File Details: PDF, 24.94 MB Year: 2012 Language: english
  • 6. iOS 5 Programming Cookbook Vandad Nahavandipoor Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo
  • 7. iOS 5 Programming Cookbook by Vandad Nahavandipoor Revision History for the : See http://oreilly.com/catalog/errata.csp?isbn=9781449311438 for release details. ISBN: 978-1-449-31143-8 1318287906
  • 8. To Agnieszka Marta Dybowska.
  • 10. Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix 1. The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Creating a Simple iOS App in Xcode 2 1.2 Understanding Interface Builder 7 1.3 Compiling iOS Apps 13 1.4 Running iOS Apps on the Simulator 15 1.5 Running iOS Apps on iOS Devices 17 1.6 Packaging iOS Apps For Distribution 21 1.7 Declaring Variables in Objective-C 27 1.8 Comparing Values in Objective-C with an If Statement 29 1.9 Implementing Loops with For Statements 32 1.10 Implementing While Loops 35 1.11 Creating Custom Classes 37 1.12 Defining Functionality for Classes 42 1.13 Defining Two or More Methods with the Same Name 46 1.14 Allocating and Initializing Objects 49 1.15 Adding Properties to Classes 51 1.16 Moving from Manual Reference Counting to Automatic Reference Counting 54 1.17 Typecasting with Automatic Reference Counting 59 1.18 Delegating Tasks with Protocols 63 1.19 Determining Whether Instance or Class Methods are Available 71 1.20 Determining Whether a Class is Available at Run Time 74 1.21 Allocating and Making Use of Strings 75 1.22 Allocating and Making Use of Numbers 79 1.23 Allocating and Making Use of Arrays 81 1.24 Allocating and Making Use of Dictionaries 86 1.25 Allocating and Making Use of Sets 89 1.26 Creating Bundles 92 1.27 Loading Data From the Main Bundle 93 1.28 Loading Data From Other Bundles 97 iii
  • 11. 1.29 Sending Notifications with NSNotificationCenter 100 1.30 Listening for Notifications Sent From NSNotificationCenter 104 2. Implementing Controllers and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 2.1 Displaying Alerts with UIAlertView 110 2.2 Creating and Using Switches with UISwitch 121 2.3 Picking Values with UIPickerView 126 2.4 Picking Date and Time with UIDatePicker 136 2.5 Implementing Range Pickers with UISlider 144 2.6 Grouping Compact Options with UISegmentedControl 151 2.7 Presenting and Managing Views with UIViewController 159 2.8 Implementing Navigation with UINavigationController 163 2.9 Manipulating a Navigation Controller’s Array of View Controllers 170 2.10 Displaying an Image on a Navigation Bar 171 2.11 Adding Buttons to Navigation Bars with UIBarButtonItem 172 2.12 Presenting Multiple View Controllers with UITabBarController 182 2.13 Displaying Static Text with UILabel 191 2.14 Accepting User Text Input with UITextField 196 2.15 Displaying Long Lines of Text with UITextView 208 2.16 Adding Buttons to the User Interface with UIButton 214 2.17 Displaying Images with UIImageView 220 2.18 Creating Scrollable Content with UIScrollView 226 2.19 Loading Web Pages with UIWebView 231 2.20 Presenting Master-Detail Views with UISplitViewController 237 2.21 Enabling Paging with UIPageViewController 245 2.22 Displaying Popovers with UIPopoverController 251 2.23 Displaying Progress with UIProgressView 262 2.24 Listening and Reacting to Keyboard Notifications 264 3. Constructing and Using Table Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 3.1 Instantiating a Table View 281 3.2 Assigning a Delegate to a Table View 284 3.3 Populating a Table View with Data 286 3.4 Receiving and Handling Table View Events 290 3.5 Using Different Types of Accessories in a Table View Cell 291 3.6 Creating Custom Table View Cell Accessories 294 3.7 Displaying Hierarchical Data in Table Views 297 3.8 Enabling Swipe Deletion of Table View Cells 299 3.9 Constructing Headers and Footers in Table Views 302 3.10 Displaying Context Menus on Table Views Cells 314 3.11 Moving Cells and Sections in Table Views 319 3.12 Deleting Cells and Sections from Table Views 326 iv | Table of Contents
  • 12. 4. Storyboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 4.1 Creating a Project with Storyboards 330 4.2 Adding a Navigation Controller to a Storyboard 333 4.3 Passing Data From One Screen to Another 346 4.4 Adding a Storyboard to an Existing Project 349 5. Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 5.1 Constructing Block Objects 358 5.2 Accessing Variables in Block Objects 362 5.3 Invoking Block Objects 369 5.4 Dispatching Tasks to Grand Central Dispatch 370 5.5 Performing UI-Related Tasks with GCD 371 5.6 Performing Non-UI-Related Tasks Synchronously with GCD 376 5.7 Performing Non-UI Related Tasks Asynchronously with GCD 379 5.8 Performing Tasks After a Delay with GCD 385 5.9 Performing a Task at Most Once with GCD 388 5.10 Grouping Tasks Together with GCD 390 5.11 Constructing Your Own Dispatch Queues with GCD 394 5.12 Running Tasks Synchronously with Operations 397 5.13 Running Tasks Asynchronously with Operations 404 5.14 Creating Dependency Between Operations 411 5.15 Creating Concurrency with Timers 413 5.16 Creating Concurrency with Threads 418 5.17 Invoking Background Methods 424 5.18 Exiting Threads and Timers 425 6. Core Location and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 6.1 Creating a Map View 431 6.2 Handling the Events of a Map View 435 6.3 Pinpointing the Location of a Device 436 6.4 Displaying Pins on a Map View 439 6.5 Displaying Pins with Different Colors on a Map View 443 6.6 Displaying Custom Pins on a Map View 449 6.7 Converting Longitude and Latitude to a Meaningful Address 452 6.8 Converting Meaningful Addresses to Longitude and Latitude 455 7. Implementing Gesture Recognizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 7.1 Detecting Swipe Gestures 459 7.2 Detecting Rotation Gestures 461 7.3 Detecting Panning and Dragging Gestures 465 7.4 Detecting Long Press Gestures 467 7.5 Detecting Tap Gestures 470 7.6 Detecting Pinch Gestures 472 Table of Contents | v
  • 13. 8. Networking, JSON, XML and Twitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 8.1 Downloading Asynchronously with NSURLConnection 475 8.2 Handling Timeouts in Asynchronous Connections 478 8.3 Downloading Synchronously with NSURLConnection 479 8.4 Modifying a URL Request with NSMutableURLRequest 482 8.5 Sending HTTP GET Requests with NSURLConnection 483 8.6 Sending HTTP POST Requests with NSURLConnection 486 8.7 Sending HTTP DELETE Requests with NSURLConnection 489 8.8 Sending HTTP PUT Requests with NSURLConnection 491 8.9 Serializing Arrays and Dictionaries into JSON 494 8.10 Deserializing JSON into Arrays and Dictionaries 496 8.11 Integrating Twitter Functionality Into Your Apps 499 8.12 Parsing XML with NSXMLParser 508 9. Audio and Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 9.1 Playing Audio Files 515 9.2 Handling Interruptions While Playing Audio 517 9.3 Recording Audio 518 9.4 Handling Interruptions While Recording Audio 525 9.5 Playing Audio over Other Active Sounds 526 9.6 Playing Video Files 530 9.7 Capturing Thumbnails from a Video File 534 9.8 Accessing the Music Library 537 10. Address Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 10.1 Getting a Reference to Address Book 550 10.2 Retrieving All the People in the Address Book 552 10.3 Retrieving Properties of Address Book Entries 553 10.4 Inserting a Person Entry into the Address Book 558 10.5 Inserting a Group Entry into the Address Book 562 10.6 Adding Persons to Groups 565 10.7 Searching the Address Book 569 10.8 Retrieving and Setting a Person’s Address Book Image 573 11. Camera and the Photo Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 11.1 Detecting and Probing the Camera 583 11.2 Taking Photos with the Camera 588 11.3 Taking Videos with the Camera 592 11.4 Storing Photos in the Photo Library 595 11.5 Storing Videos in the Photo Library 598 11.6 Retrieving Photos and Videos from the Photo Library 600 11.7 Retrieving Assets from the Assets Library 601 11.8 Editing Videos on an iOS Device 609 vi | Table of Contents
  • 14. 12. Multitasking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615 12.1 Detecting the Availability of Multitasking 616 12.2 Completing a Long-Running Task in the Background 617 12.3 Receiving Local Notifications in the Background 621 12.4 Playing Audio in the Background 630 12.5 Handling Location Changes in the Background 633 12.6 Saving and Loading the State of Multitasking iOS Apps 636 12.7 Handling Network Connections in the Background 639 12.8 Handling Notifications Delivered to a Waking App 642 12.9 Responding to Changes in App Settings 644 12.10 Opting Out of Multitasking 647 13. Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649 13.1 Creating a Core Data Model with Xcode 653 13.2 Generating Class Files for Core Data Entities 658 13.3 Creating and Saving Data Using Core Data 661 13.4 Reading Data from Core Data 663 13.5 Deleting Data From Core Data 666 13.6 Sorting Data in Core Data 669 13.7 Boosting Data Access in Table Views 671 13.8 Implementing Relationships in Core Data 683 14. Dates, Calendars and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691 14.1 Retrieving the List of Calendars 694 14.2 Adding Events to Calendars 696 14.3 Accessing the Contents of Calendars 700 14.4 Removing Events from Calendars 705 14.5 Adding Recurring Events to Calendars 709 14.6 Retrieving the Attendees of an Event 714 14.7 Adding Alarms to Calendars 719 14.8 Handling Event Changed Notifications 721 14.9 Presenting Event View Controllers 724 14.10 Presenting Event Edit View Controllers 729 15. Graphics and Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733 15.1 Enumerating and Loading Fonts 745 15.2 Drawing Text 747 15.3 Constructing, Setting, and Using Colors 749 15.4 Drawing Images 754 15.5 Drawing Lines 759 15.6 Constructing Paths 766 15.7 Drawing Rectangles 771 15.8 Adding Shadows to Shapes 776 Table of Contents | vii
  • 15. 15.9 Drawing Gradients 783 15.10 Displacing Shapes Drawn on Graphic Contexts 792 15.11 Scaling Shapes Drawn on Graphic Contexts 796 15.12 Rotating Shapes Drawn on Graphic Contexts 800 15.13 Animating and Moving Views 802 15.14 Animating and Scaling Views 812 15.15 Animating and Rotating Views 814 16. Core Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817 16.1 Detecting the Availability of an Accelerometer 819 16.2 Detecting the Availability of a Gyroscope 821 16.3 Retrieving Accelerometer Data 822 16.4 Detecting Shakes on an iOS Device 825 16.5 Retrieving Gyroscope Data 830 17. iCloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833 17.1 Setting Up Your App For iCloud 834 17.2 Storing and Synchronizing Dictionaries in iCloud 840 17.3 Creating and Managing Folders for Apps in iCloud 845 17.4 Searching for Files and Folders in iCloud 852 17.5 Storing User Documents in iCloud 862 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879 viii | Table of Contents
  • 16. Preface I started developing iPhone applications in late 2007. Between then and now, I have worked on various iPhone OS applications for different companies across the globe. As you might have already guessed, iOS is my favorite platform and Objective-C is my favorite programming language. I find that Objective-C helps programmers write clean code and iOS helps developers write user-friendly and useful applications. I have coded in other programming languages such as Assembly (using NASM and TASM) and Delphi/Pascal for many years, and I still find myself going through disas- sembled Objective-C code to find out which method of doing a certain thing or accomplishing a certain result in Objective-C is better optimized on a certain device and/or operating system. After becoming comfortable with the iOS SDK, I gradually built up a thirst to write a book about the iOS SDK, and with the help of wonderful people at O’Reilly, you are now reading the result of the several hundred hours that have been put into writing new material for iOS 3 and iOS 4, editing, reviewing, revising, and publishing. So, please go ahead and start exploring the recipes. I hope you’ll find that they are easy to cook and digest! Audience I assume you are comfortable with the iOS development environment and know how to create an app for the iPhone or iPad. This book does not get novice programmers started, but presents useful ways to get things done for iOS programmers ranging from novices to experts. Organization of This Book In this book, we will discuss frameworks and classes that are available in iOS 3 and iOS 4. In some recipes, you will find code that runs only on iOS 4 and later; in those recipes, I note that you will need the iOS 4 SDK or later to compile the example code. ix
  • 17. Here is a concise breakdown of the material each chapter covers: Chapter 1, The Basics Explains how Objective-C classes are structured and how objects can be instanti- ated. The chapter talks about properties and delegates as well as memory man- agement in Objective-C. Even if you are competent in Objective-C, I strongly suggest that you go through this chapter, even if you are skimming through it, to understand the basic material that is used in the rest of the chapters. Chapter 2, Implementing Controllers and Views Describes various approaches to constructing your iOS application’s user interface by taking advantage of different tools the SDK provides. This chapter also intro- duces you to features that are only available on the iPad, such as the popover and split view controllers. Chapter 3, Constructing and Using Table Views Shows how you can work with table views to create professional-looking iOS applications. Table views are very dynamic in nature, and as a result, programmers sometimes have difficulty understanding how they should work with them. By reading this chapter and having a look at and trying out the example code, you will gain the knowledge that is required to comfortably work with table views. Chapter 6, Core Location and Maps Describes how you should use Map Kit and Core Location APIs to develop location-aware iOS applications. First you will learn about maps, and then you will learn how to detect a device’s location and tailor your maps with custom annota- tions. You will also learn about geocoding and reverse geocoding, as well as some of the methods of the Core Location framework, which are only available in the iOS 4 SDK and later. Chapter 7, Implementing Gesture Recognizers Demonstrates how to use gesture recognizers, which enable your users to easily and intuitively manipulate the graphical interface of your iOS applications. In this chapter, you will learn how to use all available gesture recognizers in the iOS SDK, with working examples tested on iOS 3 and iOS 4 on different devices such as the iPhone 3GS, iPhone 4, and iPad. Chapter 8, Networking, JSON, XML and Twitter Demonstrates how to download data from a URL and parse XML files. You will learn about synchronous and asynchronous connections and their pros and cons. You will also learn about caching files in memory and on disk to avoid consuming the possibly limited bandwidth of an iOS device on which your application could be running. Chapter 9, Audio and Video Discusses the AV Foundation and Media Player frameworks that are available on the iOS SDK. You will learn how to play audio and video files and how to handle interruptions, such as a phone call, while the audio or video is being played on x | Preface
  • 18. both iOS 3 and iOS 4. This chapter also explains how to record audio using an iOS device’s built-in microphone(s). At the end of the chapter, you will learn how to access the iPod Library and play its media content, all from inside your application. Chapter 10, Address Book Explains the Address Book framework and how to retrieve contacts, groups, and their information from the Address Book database on an iOS device. The Address Book framework is composed entirely of C APIs. Because of this, many Objective- C developers find it difficult to use this framework compared to frameworks that provide an Objective-C interface. After reading this chapter and trying the exam- ples for yourself, you will feel much more confident using the Address Book framework. Chapter 11, Camera and the Photo Library Demonstrates how you can determine the availability of front- and back-facing cameras on an iOS device. Some of the recipes in this chapter are specific to iOS 4, with the rest working on both iOS 3 and iOS 4. You will also learn how to access the Photo Library using the Assets Library framework which is available in iOS 4 and later. At the end of the chapter, you will learn about editing videos right on an iOS device using a built-in view controller. Chapter 12, Multitasking Explains, with examples, how to create multitasking-aware applications that run beautifully on iOS 4. You will learn about background processing, from playing audio and retrieving users’ locations in the background, to downloading content from a URL while your application is running in the background. Chapter 13, Core Data Describes how to maintain persistent storage for your iOS applications using Core Data. You will learn how to add to, delete from, and edit Core Data objects and how to boost access to data in a table view. In addition, you will learn how to manage relationships between Core Data objects. Chapter 14, Dates, Calendars and Events Demonstrates the use of the Event Kit and Event Kit UI frameworks, which are available on iOS 4 and later, in order to manage calendars and events on an iOS device. You will see how to create, modify, save, and delete events. You will also learn, through examples, how to add alarms to calendar events and how to set up CalDAV calendars so that you can share a single calendar among multiple devices. Chapter 15, Graphics and Animations Introduces the Core Graphics framework. You will learn how to draw images and text on a graphics context, grab the contents of a graphics context and save it as an image, and much more. Chapter 16, Core Motion Explains the Core Motion framework, which is new in iOS 4. Using Core Motion, you will access the accelerometer and the gyroscope on an iOS device. You will also learn how to detect shakes on a device. Of course, not all iOS devices are Preface | xi
  • 19. equipped with an accelerometer and a gyroscope, so you will also learn how to detect the availability of the required hardware. Additional Resources Fromtimetotime,IrefertoofficialAppledocumentation.SomeofApple’sdescriptions are right on the mark, and there is no point in trying to restate them. Throughout this book, I have listed the most important documents and guides in the official Apple documentation that every professional iOS developer should read. For starters, I suggest that you have a look at the “iPhone Human Interface Guidelines” and the “iPad Human Interface Guidelines.” These two documents will tell you every- thing you should know about developing engaging and intuitive user interfaces for the iPhone/iPod and the iPad. Every iOS programmer must read these documents. In fact, I believe these documents must be read by the product design and development teams of any company that develops iOS applications. iPhone Human Interface Guidelines http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/ mobilehig/Introduction/Introduction.html iPad Human Interface Guidelines https://developer.apple.com/library/ios/#documentation/General/Conceptual/iPad HIG/Introduction/Introduction.html I also suggest that you skim through the “iOS Application Programming Guide” in the iOS Reference Library for some tips and advice on how to make great iOS applications: https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPho neOSProgrammingGuide/Introduction/Introduction.html One of the things you will notice when reading Chapter 12 is the use of block objects. This book concisely explains block objects, but if you require further details on the subject, I suggest you read “A Short Practical Guide to Blocks,” available at this URL: https://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide _Blocks/index.html%23/apple_ref/doc/uid/TP40009758 https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyVa lueCoding/KeyValueCoding.html Throughout this book, you will see references to “bundles” and loading images and data from bundles. You will read a concise overview about bundles in this book, but if you require further information, head over to the “Bundle Programming Guide,” avail- able at this URL: https://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptu al/CFBundles/Introduction/Introduction.html xii | Preface
  • 20. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, filenames, file extensions, and directories Constant width Indicates variables and other code elements, the contents of files, and the output from commands Constant width bold Highlights text in examples that is new or particularly significant in a recipe Constant width italic Shows text that should be replaced with user-supplied values This icon signifies a tip, suggestion, or general note. Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “iOS 4 Programming Cookbook, by Vandad Nahavandipoor (O’Reilly). Copyright 2011 Vandad Nahavandipoor, 978-1- 449-38822-5.” If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at [email protected]. We’d Like to Hear from You Every example and code snippet in this book has been tested on the iPhone 4, iPad, iPhone 3GS, and iPhone/iPad Simulator, but occasionally you may encounter problems—for example, if you have a different version of the SDK than the version on which the example code was compiled and tested. The information in this book has Preface | xiii
  • 21. also been verified at each step of the production process. However, mistakes and over- sights can occur, and we will gratefully receive details of any you find, as well as any suggestions you would like to make for future editions. You can contact the author and editors at: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 (800) 998-9938 (in the United States or Canada) (707) 829-0515 (international or local) (707) 829-0104 (fax) We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: http://oreilly.com/catalog/9781449388225 There is also a companion website to this book where you can see all the examples with color-highlighted syntax: http://www.ios4cookbook.com To comment or ask technical questions about this book, send email to the following address, mentioning the book’s ISBN number (9781449388225): [email protected] For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at: http://www.oreilly.com Safari® Books Online Safari Books Online is an on-demand digital library that lets you easily search more than 7,500 technology and creative reference books and vid- eos to find the answers you need quickly. Withasubscription,youcanreadanypageandwatchanyvideofromourlibraryonline. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, down- load chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other pub- lishers, sign up for free at http://my.safaribooksonline.com. xiv | Preface
  • 22. Acknowledgments I have always loved writing and running my programs on computers. I look at pro- gramming as a way to speak to whatever computer the program runs on. To me, programming is a way to actually connect with the computer and give it instructions and listen to what it says in return. I have been exceptionally lucky to have almost always found the right people to help me find the right path in whatever journey I’ve started in my life. First and foremost, I would like to thank my beloved fiancée, Agnieszka Marta Dybowska, for her uncon- ditional love and support throughout the years and for the many hours she had to spendwithoutmewhileIwasworkingonthisbook.Your strongenthusiasmforwriting and journalism has greatly moved me, and I sincerely hope that one day you will gather enough material to be able to write your book. I also want to thank Brian Jepson (whose many hidden talents are yet to be discovered!) for giving me a chance to work on this book. This book would have been impossible if it wasn’t for Brian’s consistent urge to improve the outline and the table of contents that I originally sent him. This reminds me to thank Andy Oram, whom I would like to call the virtual second writer of this book. Andy’s perfectionism and his undeniable desire to finely form every single sentence you read in this book are absolutely impres- sive. I must also thank Sarah Kim and Rachel James for helping me update my profile page on O’Reilly’s website. I also appreciate Meghan Blanchette’s help in doing the initial paperwork for this book. I want to say a big thank you to my technical reviewers, Eric Blair and Alasdair Allan, for all their helpful insight. Kirk Pattinson, Gary McCarville, and Sushil Shirke are among the people who have greatly influenced me to become who I am today. Thank you to Sushil for being a great mentor and colleague and for providing continuous support. Thanks to Kirk for believing that I was up to the challenge of working on some high-profile iOS applications. Thank you to Gary for his support while I worked on this project, and for being a wonderful mentor. Last but not least, I would like to sincerely thank Apple and its employees for making such a wonderful operating system and SDK. It’s truly a great pleasure to work with the iOS SDK, and I hope you, the reader, will enjoy working with it as much as I do. Preface | xv
  • 24. CHAPTER 1 The Basics 1.0 Introduction A lot has changed in iPhone, iPad, and iPod Touch programming since the introduction of iOS 5. The whole runtime and the way we write Objective-C code has dramatically changed. ARC (Automatic Reference Counting) is now introduced into the LLVM Compiler, which in some ways gives us more flexibility and in other ways makes the runtime more fragile. In this chapter, we will get down and dirty with objects and how we can use them using the modern Objective-C runtime under ARC. Objects are defined according to classes, and therefore these two terms are commonly used interchangeably. But actually, a class is just a specification for defining objects; each object is said to be an instance of its class. Each class—and therefore the objects that are created from that class—is a set of properties, tasks, methods, enumerations, and much more. In an object-oriented programming language, classes can inherit from each other much like a person can inherit certain traits and characteristics from his parents. Objective-C does not allow multiple inheritance. Therefore, every class is the direct descendant of, at most, one other class. The root class of most Objective-C objects is the NSObject class. This class manages the runtime capabilities offered by iOS; as a result, any class that directly or indirectly inherits from NSObject will inherit these capabilities as well. As we will see later in this chapter, objects that inherit from NSObject can take advantage of Objective-C’s dis- tinctive memory management model. 1
  • 25. 1.1 Creating a Simple iOS App in Xcode Problem You've started to learn iOS Programming and you want to create a really simple iOS Project and app in Xcode. Solution Create a new iOS Project in Xcode and then run it in the iOS Simulator using Command +Shift+R. Discussion I'll assume you have a Mac and you have already installed the Xcode set of tools. Now you want to create an iOS Project and run that App on the iOS Simulator. This process is really straightforward: 1. Open Xcode if you don't have it open yet. 2. Select File on the menu bar, select New, and then select New Project. You will be greeted with a screen similar to that shown in Figure 1-1. Figure 1-1. The New Project dialog in Xcode 2 | Chapter 1: The Basics
  • 26. Random documents with unrelated content Scribd suggests to you:
  • 27. Grove’s Cell, 1838. This consisted of zinc, sulphuric acid, nitric acid and platinum. It made a very powerful battery. The nitric acid is called the depolarizer as it absorbs the hydrogen gas formed, thus improving the operating voltage.
  • 28. GROVE’S BATTERY Sir William Robert Grove, an English Judge and scientist, invented a cell in 1838 consisting of a platinum electrode in strong nitric acid in a porous earthenware jar. This jar was put in dilute sulphuric acid in a glass jar in which there was an amalgamated zinc plate for the other electrode. This had an open circuit voltage of about 1.9 volts. The porous jar was used to prevent the nitric acid from attacking the zinc. The nitric acid was used for the purpose of combining with the hydrogen gas set free by the action of the sulphuric acid on the zinc, and hence was the depolarizing agent. Hydrogen combining with nitric acid forms nitrous peroxide and water. Part of the nitrous peroxide is dissolved in the water, and the rest escapes as fumes which, however, are very suffocating. The chemical equations of this cell are as follows: In outer glass jar: Zn + H2SO4 = ZnSO4 + H2 In inner porous jar: H2 + 2HNO3 = N2O4 + 2H2O An interesting thing about Grove’s cell is that it was planned in accordance with a theory. Grove knew that the electrical energy of the zinc-sulphuric acid cell came from the chemical affinity of the two reagents, and if the hydrogen gas set free could be combined with oxygen (to form water—H2O), such chemical affinity should increase the strength of the cell. As the hydrogen gas appears at the other electrode, the oxidizing agent should surround that electrode. Nitric acid was known at that time as one of the most powerful oxidizing liquids, but as it attacks copper, he used platinum for the other electrode. Thus he not only overcame the difficulty of polarization by the hydrogen gas, but also increased the voltage of the cell by the added chemical action of the combination of hydrogen and oxygen.
  • 29. GROVE’S DEMONSTRATION OF INCANDESCENT LIGHTING In 1840 Grove made an experimental lamp by attaching the ends of a coil of platinum wire to copper wires, the lower parts of which were well varnished for insulation. The platinum wire was covered by a glass tumbler, the open end set in a glass dish partly filled with water. This prevented draughts of air from cooling the incandescent platinum, and the small amount of oxygen of the air in the tumbler reduced the amount of oxidization of the platinum that would otherwise occur. With current supplied by a large number of cells of his battery, he lighted the auditorium of the Royal Institution with these lamps during one of the lectures he gave. This lamp gave only a feeble light as there was danger of melting the platinum and platinum gives but little light unless operated close to its melting temperature. It also required a lot of current to operate it as the air tended to cool the incandescent platinum. The demonstration was only of scientific interest, the cost of current being much too great (estimated at several hundred dollars a kilowatt hour) to make it commercial.
  • 30. GRENET BATTERY It was discovered that chromic anhydride gives up oxygen easier than nitric acid and consequently if used would give a higher voltage than Grove’s nitric acid battery. It also has the advantage of a lesser tendency to attack zinc directly if it happens to come in contact with it. Grenet developed a cell having a liquid consisting of a mixture of potassium bichromate (K2Cr2O7) and sulphuric acid. A porous cell was therefore not used to keep the two liquids apart. This had the advantage of reducing the internal resistance. The chemical reaction was: K_{2}Cr_{2}O_{7} (potassium bichromate) + 7H_{2}SO_{4} (sulphuric acid) + 3Zn (zinc) = 3ZnSO4 (zinc sulphate) + K2SO4 (potassium sulphate) + Cr2 (SO4)3 (chromium sulphate) + 7H2O (water). In order to prevent the useless consumption of zinc on open circuit, the zinc was attached to a sliding rod and could be drawn up into the neck of the bottle-shaped jar containing the liquid.
  • 31. Grove’s Incandescent Lamp, 1840. Grove made an experimental lamp, using platinum for the burner which was protected from draughts of air by a glass tumbler.
  • 32. DE MOLEYNS’ INCANDESCENT LAMP Frederick De Moleyns, an Englishman, has the honor of having obtained the first patent on an incandescent lamp. This was in 1841 and his lamp was quite novel. It consisted of a spherical glass globe, in the upper part of which was a tube containing powdered charcoal. This tube was open at the bottom inside the globe and through it ran a platinum wire, the end below the tube being coiled. Another platinum wire coiled at its upper end came up through the lower part of the globe but did not quite touch the other platinum coil. The powdered charcoal filled the two coils of platinum wire and bridged the gap between. Current passing through this charcoal bridge heated it to incandescence. The air in the globe having been removed as far as was possible with the hand air pumps then available, the charcoal did not immediately burn up, the small amount consumed being replaced by the supply in the tube. The idea was ingenious but the lamp was impractical as the globe rapidly blackened from the evaporation of the incandescent charcoal.
  • 33. De Moleyns’ Incandescent Lamp, 1841. This consisted of two coils of platinum wire containing powdered charcoal operating in a vacuum. It is only of interest as the first incandescent lamp on which a patent (British) was granted.
  • 34. EARLY DEVELOPMENTS OF THE ARC LAMP It had been found that most of the light of the arc came from the tip of the positive electrode, and that the charcoal electrodes were rapidly consumed, the positive electrode about twice as fast as the negative. Mechanisms were designed to take care of this, together with devices to start the arc by allowing the electrodes to touch each other and then pulling them apart the proper distance. This distance varied from one-eighth to three-quarters of an inch. In 1840 Bunsen, the German chemist who invented the bunsen burner, devised a process for making hard dense carbon pencils which lasted much longer than the charcoal previously used. The dense carbon from the inside of the retorts of gas making plants was ground up and mixed with molasses, moulded into shape and baked at a high temperature. Bunsen also, in 1843, cheapened Grove’s battery by substituting a hard carbon plate in place of the platinum electrode.
  • 35. Wright’s Arc Lamp, 1845. This lamp is also only of interest as the first arc lamp on which a patent (British) was granted. Four arcs played between the five carbon discs. Thomas Wright, an Englishman, was the first to patent an arc lamp. This was in 1845, and the lamp was a hand regulated device consisting of five carbon disks normally touching each other and rotated by clockwork. Two of the disks could be drawn outward by thumb screws, which was to be done after the current was turned on thus establishing four arcs, one between each pair of disks. The next year, 1846, W. E. Staite, another Englishman, made an arc lamp having two vertical carbon pencils. The upper was stationary. The lower was movable and actuated by clockwork directed by ratchets which in turn were regulated by an electro-magnet controlled by the current flowing through the arc. Thus the lower carbon would be moved up or down as required. Archereau, a Frenchman, made a very simple arc lamp in 1848. The upper carbon was fixed and the lower one was mounted on a piece of iron which could be drawn down into a coil of wire. The weight of the lower electrode was overbalanced by a counterweight, so that when no current was flowing the two carbons would touch. When current was turned on, it flowed through the two carbons and through the coil of wire (solenoid) which then became energized and
  • 36. pulled the lower carbon down, thus striking the arc. Two of these arc lamps were installed in Paris and caused considerable excitement. After a few weeks of unreliable operation, it was found that the cost of current from the batteries was much too great to continue their use commercially. The dynamo had not progressed far enough to permit its use. Archereau’s Arc Lamp, 1848. This simple arc was controlled by an electro- magnet, and two lamps were installed for street lighting in Paris, current being obtained from batteries.
  • 37. JOULE’S LAW Joule was an Englishman, and in 1842 began investigating the relation between mechanical energy and heat. He first showed that, by allowing a weight to drop from a considerable height and turn a paddle wheel in water, the temperature of the water would increase in relation to the work done in turning the wheel. It is now known that 778 foot-pounds (1 lb. falling 778 feet, 10 lbs. falling 77.8 feet or 778 lbs. falling one foot, etc.) is the mechanical equivalent of energy equal to raising one pound of water one degree Fahrenheit. The rate of energy (power) is the energy divided by a unit of time; thus one horsepower is 33,000 foot-pounds per minute. Joule next investigated the relation between heat and electric current. He made a device consisting of a vessel of water in which there were a thermometer and an insulated coil of wire having a considerable resistance. He found that an electric current heated the water, and making many combinations of the amount and length of time of current flowing and of the resistance of the wire, he deduced the law that the energy in an electric circuit is proportional to the square of the amount of current flowing multiplied by the length of time and multiplied by the resistance of the wire. The rate of electrical energy (electric power) is therefore proportional to the square of current multiplied by the resistance. The electrical unit of power is now called the Watt, named in honor of James Watt, the Englishman, who made great improvements to the steam engine about a century ago. Thus, watts = C2R and substituting the value of R from Ohm’s law, C = E/R, we get Watts = Volts × Amperes
  • 38. The watt is a small unit of electric power, as can be seen from the fact that 746 watts are equal to one horsepower. The kilowatt, kilo being the Greek word for thousand, is 1000 watts. This term is an important one in the electrical industry. For example, dynamos are rated in kilowatts, expressed as KW; the largest one made so far is 50,000 KW which is 66,666 horsepower. Edison’s first commercial dynamo had a capacity of 6 KW although the terms watts and kilowatts were not in use at that time. The ordinary sizes of incandescent lamps now used in the home are 25, 40 and 50 watts.
  • 39. STARR’S INCANDESCENT LAMP Starr’s Incandescent Lamp, 1845. This consisted of a short carbon pencil operating in the vacuum above a column of mercury. J. W. Starr, an American, of Cincinnati, Ohio, assisted financially by Peabody, the philanthropist, went to England where he obtained a patent in 1845 on the lamps he had invented, although the patent was taken out under the name of King, his attorney. One is of passing interest only. It consisted of a strip of platinum, the active length of which could be adjusted to fit the battery strength used, and was covered by a glass globe to protect it from draughts of air. The other, a carbon lamp, was the first real contribution to the art. It consisted of a rod of carbon operating in the vacuum above a column of mercury (Torrecellium vacuum) as in a barometer. A heavy
  • 40. platinum wire was sealed in the upper closed end of a large glass tube, and connected to the carbon rod by an iron clamp. The lower end of the carbon rod was fastened to another iron clamp, the two clamps being held in place and insulated from each other by a porcelain rod. Attached to the lower clamp was a long copper wire. Just below the lower clamp, the glass tube was narrowed down and had a length of more than 30 inches. The tube was then filled with mercury, the bottom of the tube being put into a vessel partly full of mercury. The mercury ran out of the enlarged upper part of the tube, coming to rest in the narrow part of the tube as in a barometer, so that the carbon rod was then in a vacuum. One lamp terminal was the platinum wire extending through the top of the tube, and the other was the mercury. Several of these lamps were put on exhibition in London, but were not a commercial success as they blackened very rapidly. Starr started his return trip to the United States the next year, but died on board the ship when he was but 25 years old.
  • 41. OTHER EARLY INCANDESCENT LAMPS Staite’s Incandescent Lamp, 1848. The burner was of platinum and iridium.
  • 42. Roberts’ Incandescent Lamp, 1852. It had a graphite burner operating in vacuum. In 1848 W. E. Staite, who two years previously had made an arc lamp, invented an incandescent lamp. This consisted of a platinum- iridium burner in the shape of an inverted U, covered by a glass globe. It had a thumb screw for a switch, the whole device being mounted on a bracket which was used for the return wire. E. C. Shepard, another Englishman, obtained a patent two years later on an incandescent lamp consisting of a weighted hollow charcoal cylinder the end of which pressed against a charcoal cone. Current passing through this high resistance contact, heated the charcoal to incandescence. It operated in a glass globe from which the air could be exhausted. M. J. Roberts obtained an English patent in 1852 on an incandescent lamp. This had a graphite rod for a burner, which could be renewed, mounted in a glass globe. The globe was cemented to a metallic cap fastened to a piece of pipe through which the air could be exhausted. After being exhausted, the pipe, having a stop cock, could be screwed on a stand to support the lamp. Moses G. Farmer, a professor at the Naval Training Station at Newport, Rhode Island, lighted the parlor of his home at 11 Pearl Street, Salem, Mass., during July, 1859, with several incandescent lamps having a strip of platinum for the burner. The novel feature of
  • 43. this lamp was that the platinum strip was narrower at the terminals than in the center. Heat is conducted away from the terminals and by making the burner thin at these points, the greater resistance of the ends of the burner absorbed more electrical energy thus offsetting the heat being conducted away. This made a more uniform degree of incandescence throughout the length of the burner, and Prof. Farmer obtained a patent on this principle many years later (1882). Farmer’s Incandescent Lamp, 1859. This experimental platinum lamp was made by Professor Farmer and several of them lighted the parlor of his home in Salem, Mass.
  • 44. FURTHER ARC LAMP DEVELOPMENTS During the ten years, 1850 to 1860, several inventors developed arc lamp mechanisms. Among them was M. J. Roberts, who had invented the graphite incandescent lamp. In Roberts’ arc lamp, which he patented in 1852, the lower carbon was stationary. The upper carbon fitted snugly into an iron tube. In the tube was a brass covered iron rod, which by its weight could push the upper carbon down the tube so the two carbons normally were in contact. An electro-magnet in series with the arc was so located that, when energized, it pulled up the iron tube. This magnet also held the brass covered iron rod from pushing the upper carbon down the tube so that the two carbons were pulled apart, striking the arc. When the arc went out, the iron tube dropped back into its original position, the brass covered iron rod was released, pushing the upper carbon down the tube until the two carbons again touched. This closed the circuit again, striking the arc as before.
  • 45. Roberts’ Arc Lamp, 1852. The arc was controlled by an electro-magnet which held an iron tube to which the upper carbon was fastened. Slater and Watson’s Arc Lamp, 1852. Clutches were used for the first time in this arc lamp to feed the carbons. In the same year (1852) Slater and Watson obtained an English patent on an arc lamp in which the upper carbon was movable and
  • 46. held in place by two clutches actuated by electro-magnets. The lower carbon was fixed, and normally the two carbons touched each other. When current was turned on, the electro-magnet lifted the clutches which gripped the upper carbon, pulling it up and striking the arc. This was the first time that a clutch was used to allow the carbon to feed as it became consumed. Henry Chapman, in 1855, made an arc in which the upper carbon was allowed to feed by gravity, but held in place by a chain wound around a wheel. On this wheel was a brake actuated by an electro-magnet. The lower carbon was pulled down by an electro- magnet working against a spring. When no current was flowing or when the arc went out, the two carbons touched. With current on, one electro-magnet set the brake and held the upper carbon stationary. The other electro-magnet pulled the lower carbon down, thus striking the arc. None of these mechanisms regulated the length of the arc. It was not until 1856 that Joseph Lacassagne and Henry Thiers, Frenchmen, invented the so-called “differential” method of control, which made the carbons feed when the arc voltage, and hence length, became too great. This principle was used in commercial arc lamps several years afterward when they were operated on series circuits, as it had the added advantage of preventing the feeding of one arc lamp affecting another on the same circuit. This differential control consists in principle of two electro-magnets, one in series with, and opposing the pull of the other which is in shunt with the arc. The series magnet pulls the carbons apart and strikes the arc. As the arc increases in length, its voltage rises, thereby increasing the current flowing through the shunt magnet. This increases the strength of the shunt magnet and, when the arc becomes too long, the strength of the shunt becomes greater than that of the series magnet, thus making the carbons feed.
  • 47. Welcome to our website – the ideal destination for book lovers and knowledge seekers. With a mission to inspire endlessly, we offer a vast collection of books, ranging from classic literary works to specialized publications, self-development books, and children's literature. Each book is a new journey of discovery, expanding knowledge and enriching the soul of the reade Our website is not just a platform for buying books, but a bridge connecting readers to the timeless values of culture and wisdom. With an elegant, user-friendly interface and an intelligent search system, we are committed to providing a quick and convenient shopping experience. Additionally, our special promotions and home delivery services ensure that you save time and fully enjoy the joy of reading. Let us accompany you on the journey of exploring knowledge and personal growth! ebookfinal.com