1. iOS 7 Programming Cookbook 2nd Edition Vandad
Nahavandipoor pdf download
https://ebookname.com/product/ios-7-programming-cookbook-2nd-
edition-vandad-nahavandipoor/
Get the full ebook with Bonus Features for a Better Reading Experience on ebookname.com
2. Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...
iOS 4 Programming Cookbook Solutions Examples for
iPhone iPad and iPod touch Apps 1st Edition Vandad
Nahavandipoor
https://ebookname.com/product/ios-4-programming-cookbook-
solutions-examples-for-iphone-ipad-and-ipod-touch-apps-1st-
edition-vandad-nahavandipoor/
Writing Game Center Apps in iOS Bringing Your Players
Into the Game 1st Edition Vandad Nahavandipoor
https://ebookname.com/product/writing-game-center-apps-in-ios-
bringing-your-players-into-the-game-1st-edition-vandad-
nahavandipoor/
iOS and OS X Network Programming Cookbook 1st Edition
Jon Hoffman
https://ebookname.com/product/ios-and-os-x-network-programming-
cookbook-1st-edition-jon-hoffman/
Handbook of Ecological and Ecosystem Engineering 1st
Edition Majeti Narasimha Vara Prasad
https://ebookname.com/product/handbook-of-ecological-and-
ecosystem-engineering-1st-edition-majeti-narasimha-vara-prasad/
3. Case Files Pediatrics Sixth Edition Eugene C. Toy
https://ebookname.com/product/case-files-pediatrics-sixth-
edition-eugene-c-toy/
Water Infrastructure Issues 1st Edition James D.
Haffner
https://ebookname.com/product/water-infrastructure-issues-1st-
edition-james-d-haffner/
American Sign Language the Easy Way 2nd Edition David
A. Stewart
https://ebookname.com/product/american-sign-language-the-easy-
way-2nd-edition-david-a-stewart/
The Farther Adventures of Robinson Crusoe The Stoke
Newington Edition Daniel Defoe (Editor)
https://ebookname.com/product/the-farther-adventures-of-robinson-
crusoe-the-stoke-newington-edition-daniel-defoe-editor/
Problem Solved A Primer for Design and Communication
First Edition Michael Johnson
https://ebookname.com/product/problem-solved-a-primer-for-design-
and-communication-first-edition-michael-johnson/
4. The hydrogen economy Opportunities and challenges
Michael Ball
https://ebookname.com/product/the-hydrogen-economy-opportunities-
and-challenges-michael-ball/
9. Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1. Implementing Controllers and Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1. Displaying Alerts with UIAlertView 23
1.2. Creating and Using Switches with UISwitch 32
1.3. Customizing the UISwitch 36
1.4. Picking Values with the UIPickerView 39
1.5. Picking the Date and Time with UIDatePicker 45
1.6. Implementing Range Pickers with UISlider 50
1.7. Customizing the UISlider 54
1.8. Grouping Compact Options with UISegmentedControl 59
1.9. Presenting and Managing Views with UIViewController 63
1.10. Presenting Sharing Options with UIActivityViewController 67
1.11. Presenting Custom Sharing Options with UIActivityViewController 73
1.12. Implementing Navigation with UINavigationController 79
1.13. Manipulating a Navigation Controller’s Array of View Controllers 85
1.14. Displaying an Image on a Navigation Bar 86
1.15. Adding Buttons to Navigation Bars Using UIBarButtonItem 88
1.16. Presenting Multiple View Controllers with UITabBarController 94
1.17. Displaying Static Text with UILabel 101
1.18. Customizing the UILabel 105
1.19. Accepting User Text Input with UITextField 108
1.20. Displaying Long Lines of Text with UITextView 118
1.21. Adding Buttons to the User Interface with UIButton 123
1.22. Displaying Images with UIImageView 127
1.23. Creating Scrollable Content with UIScrollView 132
1.24. Loading Web Pages with UIWebView 137
1.25. Displaying Progress with UIProgressView 141
1.26. Constructing and Displaying Styled Texts 143
iii
10. 1.27. Presenting Master-Detail Views with UISplitViewController 148
1.28. Enabling Paging with UIPageViewController 153
1.29. Displaying Popovers with UIPopoverController 158
2. Creating Dynamic and Interactive User Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
2.1. Adding Gravity to Your UI Components 171
2.2. Detecting and Reacting to Collisions Between UI Components 172
2.3. Animating Your UI Components with a Push 180
2.4. Attaching Multiple Dynamic Items to Each Other 184
2.5. Adding a Dynamic Snap Effect to Your UI Components 189
2.6. Assigning Characteristics to Your Dynamic Effects 192
3. Auto Layout and the Visual Format Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
3.1. Placing UI Components in the Center of the Screen 201
3.2. Defining Horizontal and Vertical Constraints with the Visual Format
Language 203
3.3. Utilizing Cross View Constraints 210
3.4. Configuring Auto Layout Constraints in Interface Builder 217
4. Constructing and Using Table Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
4.1. Populating a Table View with Data 225
4.2. Using Different Types of Accessories in a Table View Cell 229
4.3. Creating Custom Table View Cell Accessories 232
4.4. Enabling Swipe Deletion of Table View Cells 235
4.5. Constructing Headers and Footers in Table Views 237
4.6. Displaying Context Menus on Table View Cells 246
4.7. Moving Cells and Sections in Table Views 251
4.8. Deleting Cells and Sections from Table Views 257
4.9. Utilizing the UITableViewController for Easy Creation of Table Views 268
4.10. Displaying a Refresh Control for Table Views 274
5. Building Complex Layouts with Collection Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
5.1. Constructing Collection Views 281
5.2. Assigning a Data Source to a Collection View 284
5.3. Providing a Flow Layout to a Collection View 285
5.4. Providing Basic Content to a Collection View 288
5.5. Feeding Custom Cells to Collection Views Using .xib Files 294
5.6. Handling Events in Collection Views 299
5.7. Providing a Header and a Footer in a Flow Layout 303
5.8. Adding Custom Interactions to Collection Views 308
iv | Table of Contents
11. 5.9. Providing Contextual Menus on Collection View Cells 311
6. Storyboards. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
6.1. Adding a Navigation Controller to a Storyboard 316
6.2. Passing Data from One Screen to Another 318
6.3. Adding a Tab Bar Controller to a Storyboard 325
6.4. Introducing Custom Segue Transitions to Your Storyboard 328
6.5. Placing Images and Other UI Components on Storyboards 332
7. Concurrency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
7.1. Constructing Block Objects 342
7.2. Accessing Variables in Block Objects 346
7.3. Invoking Block Objects 352
7.4. Performing UI-Related Tasks with GCD 354
7.5. Executing Non-UI Related Tasks Synchronously with GCD 358
7.6. Executing Non-UI Related Tasks Asynchronously with GCD 361
7.7. Performing Tasks after a Delay with GCD 368
7.8. Performing a Task Only Once with GCD 371
7.9. Grouping Tasks Together with GCD 373
7.10. Constructing Your Own Dispatch Queues with GCD 377
7.11. Running Tasks Synchronously with Operations 380
7.12. Running Tasks Asynchronously with Operations 387
7.13. Creating Dependency Between Operations 393
7.14. Creating Timers 395
7.15. Creating Concurrency with Threads 400
7.16. Invoking Background Methods 406
7.17. Exiting Threads and Timers 407
8. Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
8.1. Enabling Security and Protection for Your Apps 418
8.2. Storing Values in the Keychain 422
8.3. Finding Values in the Keychain 424
8.4. Updating Existing Values in the Keychain 429
8.5. Deleting Exiting Values in the Keychain 432
8.6. Sharing Keychain Data Between Multiple Apps 434
8.7. Writing to and Reading Keychain Data from iCloud 440
8.8. Storing Files Securely in the App Sandbox 443
8.9. Securing Your User Interface 446
9. Core Location and Maps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
9.1. Creating a Map View 450
9.2. Handling the Events of a Map View 452
Table of Contents | v
12. 9.3. Pinpointing the Location of a Device 453
9.4. Displaying Pins on a Map View 455
9.5. Displaying Pins with Different Colors on a Map View 459
9.6. Displaying Custom Pins on a Map View 465
9.7. Converting Meaningful Addresses to Longitude and Latitude 468
9.8. Converting Longitude and Latitude to a Meaningful Address 470
9.9. Searching on a Map View 472
9.10. Displaying Directions on the Map 475
10. Implementing Gesture Recognizers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
10.1. Detecting Swipe Gestures 483
10.2. Detecting Rotation Gestures 485
10.3. Detecting Panning and Dragging Gestures 489
10.4. Detecting Long-Press Gestures 491
10.5. Detecting Tap Gestures 495
10.6. Detecting Pinch Gestures 497
11. Networking, JSON, XML, and Sharing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
11.1. Downloading Asynchronously with NSURLConnection 501
11.2. Handling Timeouts in Asynchronous Connections 504
11.3. Downloading Synchronously with NSURLConnection 506
11.4. Modifying a URL Request with NSMutableURLRequest 508
11.5. Sending HTTP GET Requests with NSURLConnection 509
11.6. Sending HTTP POST Requests with NSURLConnection 511
11.7. Sending HTTP DELETE Requests with NSURLConnection 513
11.8. Sending HTTP PUT Requests with NSURLConnection 514
11.9. Serializing Arrays and Dictionaries into JSON 516
11.10. Deserializing JSON into Arrays and Dictionaries 518
11.11. Integrating Social Sharing into Your Apps 521
11.12. Parsing XML with NSXMLParser 525
12. Audio and Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
12.1. Playing Audio Files 531
12.2. Handling Interruptions While Playing Audio 534
12.3. Recording Audio 535
12.4. Handling Interruptions While Recording Audio 542
12.5. Playing Audio over Other Active Sounds 543
12.6. Playing Video Files 547
12.7. Capturing Thumbnails from Video Files 551
12.8. Accessing the Music Library 554
13. Address Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
vi | Table of Contents
13. 13.1. Requesting Access to the Address Book 564
13.2. Retrieving a Reference to an Address Book 568
13.3. Retrieving All the People in the Address Book 571
13.4. Retrieving Properties of Address Book Entries 573
13.5. Inserting a Person Entry into the Address Book 577
13.6. Inserting a Group Entry into the Address Book 581
13.7. Adding Persons to Groups 584
13.8. Searching the Address Book 587
13.9. Retrieving and Setting a Person’s Address Book Image 592
14. Files and Folder Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
14.1. Finding the Paths of the Most Useful Folders on Disk 603
14.2. Writing to and Reading from Files 605
14.3. Creating Folders on Disk 610
14.4. Enumerating Files and Folders 612
14.5. Deleting Files and Folders 618
14.6. Saving Objects to Files 621
15. Camera and the Photo Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
15.1. Detecting and Probing the Camera 627
15.2. Taking Photos with the Camera 632
15.3. Taking Videos with the Camera 636
15.4. Storing Photos in the Photo Library 639
15.5. Storing Videos in the Photo Library 644
15.6. Retrieving Photos and Videos from the Photo Library 646
15.7. Retrieving Assets from the Assets Library 649
15.8. Editing Videos on an iOS Device 656
16. Multitasking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
16.1. Detecting the Availability of Multitasking 664
16.2. Completing a Long-Running Task in the Background 665
16.3. Adding Background Fetch Capabilities to Your Apps 669
16.4. Playing Audio in the Background 678
16.5. Handling Location Changes in the Background 682
16.6. Saving and Loading the State of Multitasking Apps 684
16.7. Handling Network Connections in the Background 688
16.8. Opting Out of Multitasking 691
17. Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693
17.1. Sending Notifications 694
17.2. Listening for and Reacting to Notifications 696
17.3. Listening and Reacting to Keyboard Notifications 700
Table of Contents | vii
14. 17.4. Scheduling Local Notifications 707
17.5. Listening for and Reacting to Local Notifications 711
17.6. Handling Local System Notifications 714
17.7. Setting Up Your App for Push Notifications 718
17.8. Delivering Push Notifications to Your App 724
17.9. Reacting to Push Notifications 732
18. Core Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
18.1. Creating a Core Data Model with Xcode 737
18.2. Generating Class Files for Core Data Entities 741
18.3. Creating and Saving Data Using Core Data 745
18.4. Reading Data from Core Data 747
18.5. Deleting Data from Core Data 750
18.6. Sorting Data in Core Data 752
18.7. Boosting Data Access in Table Views 754
18.8. Implementing Relationships in Core Data 761
18.9. Fetching Data in the Background 768
18.10. Using Custom Data Types in Your Core Data Model 772
19. Dates, Calendars, and Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
19.1. Requesting Permission to Access Calendars 784
19.2. Retrieving Calendar Groups on an iOS Device 790
19.3. Adding Events to Calendars 792
19.4. Accessing the Contents of Calendars 796
19.5. Removing Events from Calendars 799
19.6. Adding Recurring Events to Calendars 803
19.7. Retrieving the Attendees of an Event 808
19.8. Adding Alarms to Calendars 811
19.9. Handling Event Changed Notifications 814
19.10. Presenting Event View Controllers 816
19.11. Presenting Event Edit View Controllers 822
20. Graphics and Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827
20.1. Enumerating and Loading Fonts 833
20.2. Drawing Text 835
20.3. Constructing, Setting, and Using Colors 836
20.4. Drawing Images 841
20.5. Constructing Resizable Images 845
20.6. Drawing Lines 850
20.7. Constructing Paths 858
20.8. Drawing Rectangles 862
20.9. Adding Shadows to Shapes 866
viii | Table of Contents
15. 20.10. Drawing Gradients 873
20.11. Moving Shapes Drawn on Graphic Contexts 882
20.12. Scaling Shapes Drawn on Graphic Contexts 886
20.13. Rotating Shapes Drawn on Graphic Contexts 889
20.14. Animating and Moving Views 890
20.15. Animating and Scaling Views 900
20.16. Animating and Rotating Views 901
20.17. Capturing a Screenshot of Your View into an Image 903
21. Core Motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907
21.1. Detecting the Availability of an Accelerometer 908
21.2. Detecting the Availability of a Gyroscope 910
21.3. Retrieving Accelerometer Data 911
21.4. Detecting Shakes on an iOS Device 915
21.5. Retrieving Gyroscope Data 916
22. iCloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
22.1. Setting Up Your App for iCloud 920
22.2. Storing and Synchronizing Dictionaries in iCloud 924
22.3. Creating and Managing Folders for Apps in iCloud 929
22.4. Searching for Files and Folders in iCloud 936
22.5. Storing User Documents in iCloud 946
22.6. Managing the State of Documents in iCloud 961
23. Pass Kit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
23.1. Creating Pass Kit Certificates 968
23.2. Creating Pass Files 975
23.3. Providing Icons and Images for Passes 984
23.4. Preparing Your Passes for Digital Signature 987
23.5. Signing Passes Digitally 989
23.6. Distributing Passes Using Email 993
23.7. Distributing Passes Using Web Services 995
23.8. Enabling Your iOS Apps to Access Passes on iOS Devices 997
23.9. Interacting with Passbook Programmatically 1003
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1007
Table of Contents | ix
17. Preface
This edition of the book is not just an update, but a total remake of the previous edition.
iOS 7 changed everything: the look and feel, the way we use our iOS devices, and most
importantly, the way we program for iOS devices. This called for a substantial rewrite
indeed. I have added roughly 50 new recipes to this book, talking about things such as
UIKit dynamics, collection views, the keychain, push notifications, and whatnot. I have
also gone through all the example codes and figures and updated them for iOS 7.
iOS 7 is a huge step forward for this amazing operating system that we all, as program‐
mers and users, have grown to love and enjoy programming for. You must have noticed
how the focus of iOS 7 is on being dynamic: how your UI should adapt to various
movements and motions that can be applied to the device. What I mean by that is Apple
wants developers to really look at the details of their apps and bring real-world physics
and dynamics into them. That’s why Apple introduced UIKit Dynamics to the SDK, and
that is why this book has a whole chapter dedicated to this concept. The more expensive
a high-end device such as the new iPhone becomes, the more demanding the users will
get as well. Nobody blames them, though! They have just bought a fantastic and top-
of-the-line new iPhone or iPad and they want to see amazing apps running on them,
leveraging all the capabilities that those devices have to offer.
That is why now more than ever developers have to get an in-depth knowledge of the
SDK and what the SDK has to offer to the developers so that we can create better and
faster apps for iOS users. Apple introduced a lot of cool new APIs to the iOS 7 SDK, and
we are going to explore them in this book.
The focus of iOS 7 is dynamics!
Before you read about this book, maybe you’d like to know about my background a bit
and how I can help you through this journey. I will just briefly let you know who I am
and how I got to love iOS. I started out writing Basic code for my Commodore 64 when
Iwasakid.IthenmovedontobuymyownPCandstartedexperimentingwithAssembly
code. At first, it was 8-bit Assembly for DOS. I then moved onto writing my own hobby
xi
18. operating system, which was never really released as a commercial product, for 32-bit
Intel x86 CPU architectures.
Among all the programming languages that I have programmed in, Assembly and
Objective-C are the two that have really been different from the others, and I’ve really
liked them. Assembly because of the purity of the language: a command does only one
thing and does that one thing well. I believe that I like Objective-C for the same reason,
and in fact iOS shares the same trait with Assembly and Objective-C. Even though iOS
is an operating system and not a programming language, whatever it does, it does it best
and better than its rivals. From its simplicity to the sheer power that you can harvest
from the software and the hardware combined, using technologies such as GCD, the
bar that iOS has set in terms of ease of use and beauty is unprecedented.
This edition of the book has seen all the recipes inside all chapters completely renewed
for iOS 7. All screenshots have been updated, and many more recipes—such as those
related to security and the keychain, UI dynamics, collection views, push and local
notifications, and many more—have exclusively been written for this edition of the
book. I really have had a fun time writing this edition of the book, and packed as it is
with new features, I hope you’ll enjoy reading it. May it be a valuable addition to your
tech-book library.
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 the iOS 7 SDK.
This book does its best to teach you the latest and the greatest APIs. As you know, some
users of your apps may still be on older versions of iOS, so please consider those users
and choose your APIs wisely, depending on the minimum iOS version that you want to
target with your apps.
Apple has recommended that you write your apps so that they support and run on iOS
6 and iOS 7. This means you need to use the latest SDK as your base SDK (the SDK that
youusetocompileyourapp)andchooseiOS6asyourtarget,ifthat’swhatyourbusiness
requirements dictate. If you are required to write your app to support only iOS 7, then
you are in for a lot of fun, as you can use all the cool APIs that have been introduced in
iOS 7 and discussed in this book.
xii | Preface
19. Here is a concise breakdown of the material each chapter covers:
Chapter 1, Implementing Controllers and Views
Explains how Objective-C classes are structured and how objects can be instanti‐
ated. The chapter talks about properties and delegates and subscripting by keys and
indexes. Even if you are competent in Objective-C, I strongly suggest that you read
this chapter, even if you only skim through it, to understand the basic material that
is used in the rest of the book. In this chapter, we will also explore the common
usage of various UI components, such as alert views, segmented controls, switches,
and labels. We will also talk about customizing these components with the latest
APIs provided in the SDK.
Chapter 2, Creating Dynamic and Interactive User Interfaces
Talks about UIKit Dynamics, the newest addition to the UIKit framework. These
dynamics allow you to add real-life physics and dynamics to your UI components.
This will allow you to create even livelier user interfaces with very small effort on
your side.
Chapter 3, Auto Layout and the Visual Format Language
Explains how you can take advantage of Auto Layout in the iOS SDK in order to
construct your UI in such a way that it can be resized and stretched to pretty much
any screen dimension.
Chapter 4, Constructing and Using Table Views
Shows how you can work with table views to create professional-looking iOS ap‐
plications. 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 trying out the example code, you will gain the knowledge
that is required to comfortably work with table views.
Chapter 5, Building Complex Layouts with Collection Views
Collection views have been available to OS X programmers for quite some time
now, and Apple decided to provide the same APIs to iOS programmers in the iOS
SDK. Collection views are very much like table views, but they are much more
configurable and dynamic. Where in table views we have the concept of sections
and rows in each section, collection views bring columns to the equation as well,
so that you can display many items in one row if you want to. In this chapter we
will have a look at all the great user interfaces that you can create using collection
views.
Chapter 6, Storyboards
Demonstrates the process of storyboarding, the new way to define the connections
between different screens in your app. The great thing about storyboarding is that
you don’t have to know anything about iOS programming to get a simple app run‐
ning. This helps product analysts, product owners, or designers who work inde‐
pendently of developers to gain knowledge of the UI components iOS offers and to
Preface | xiii
20. buildmorerobustproducts.Programmerscanalsotakeadvantageofstoryboarding
to easily create prototypes. Storyboarding is just fun, whether you do it on paper
or using Xcode.
Chapter 7, Concurrency
As humans, we can do many things simultaneously without thinking much about
it. With advances in computer technology, mobile devices are also able to multitask,
and they provide programmers with tools and mechanisms that can accomplish
more than one task at the same time. This is called concurrency. In this chapter, you
will learn about Grand Central Dispatch, Apple’s preferred way of achieving con‐
currency in iOS. You will also learn about timers, threads, and operations.
Chapter 8, Security
iOS is a very secure operating system, and apps that we write for it also have to
adhere to certain security standards and practices. In this chapter, we will discuss
how you can take advantage of keychain APIs to make your apps more secure. We
will also talk about various steps that you can take to make your user interface more
secure.
Chapter 9, Core Location and Maps
DescribeshowyoushoulduseMapKitandCoreLocationAPIstodeveloplocation-
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 annotations.
You will also learn about geocoding and reverse geocoding, as well as some of the
methods of the Core Location framework, which are available only in iOS 7.
Chapter 10, 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 7.
Chapter 11, Networking, JSON, XML, and Sharing
Demonstrates the built-in JSON and XML parsers. On top of that, this chapter talks
about various networking APIs and how programmers can build social networking
into our apps to allow our users to share their creations and data to social networks
such as Facebook.
Chapter 12, 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. This
chapter also explains how to record audio using an iOS device’s built-in micro‐
phone(s). At the end of the chapter, you will learn how to access the Music Library
and play its media content, all from inside your application.
xiv | Preface
21. Chapter 13, 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, as compared with frameworks
that provide an Objective-C interface. After reading this chapter and trying the
examples for yourself, you will feel much more confident using the Address Book
framework.
Chapter 14, Files and Folder Management
One of the most important tasks that, as developers, we want to perform in our iOS
apps is manipulating files and folders. Whether this means creating, reading from,
writing to, or deleting them, this chapter contains enough material to get you up
and running with file and folder management in the iOS SDK.
Chapter 15, Camera and the Photo Library
Demonstrates how you can determine the availability of front- and back-facing
cameras on an iOS device. You will also learn how to access the photo library using
the Assets Library framework. At the end of the chapter, you will learn about editing
videos right on an iOS device using a built-in view controller.
Chapter 16, Multitasking
Showsmultitasking-awareapplicationsthatrunbeautifullyoniOSdevices.Youwill
learn about background processing, including how to play audio and retrieve users’
locations in the background, as well as how to download content from a URL while
your application is running in the background. On top of that, we will explore some
of the new APIs that iOS 7 provides to us, in order to enable our apps to download
content periodically while in the background or even while our app is not even
running.
Chapter 17, Notifications
Notificationsareobjectsthatcanbecomposedbyasourceanddeliveredtomultiple
recipients.Inthischapter,wewilldiscussnotifications,includinglocalnotifications
and push notifications, along with how you can use the latest capabilities built into
Xcode to easily enable these features in your own apps.
Chapter 18, Core Data
Describes the details of Core Data stacks and what they are made out of. You will
thenbeabletodesignyourownobject-orienteddatamodelsrightintoXcode,using
the Core Data model editor, and also create and retrieve your objects in Core Data.
On top of that, you will learn how to add your own custom data to Core Data and
how to search for data in the background thread, leaving your UI thread ready to
process user events.
Preface | xv
22. Chapter 19, Dates, Calendars, and Events
Demonstrates the use of the Event Kit and Event Kit UI frameworks 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 20, Graphics and Animations
Introduces the Core Graphics framework. You will learn how to draw images and
text on a graphics context; draw lines, rectangles, and paths; and much more. You
will also learn to use the new iOS SDK APIs to capture your views’ contents as
screenshots.
Chapter 21, Core Motion
Explains the Core Motion framework. Using Core Motion, you will access the ac‐
celerometer 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 equipped with an accelerom‐
eter and a gyroscope, so you will also learn how to detect the availability of the
required hardware.
Chapter 22, iCloud
Shows how to use the iCloud service, which ties devices together and allows them
to share data to provide a seamless user experience as the user moves from one
device to another.
Chapter 23, Pass Kit
Describes Passbook: a virtual wallet, if you will, capable of managing your coupons,
boarding passes, rail and bus tickets, and much more. In this chapter, you will learn
all there is to know in order to be able to create your own digitally signed passes
and distribute them to your users easily.
Additional Resources
From time to time, I refer to official Apple documentation. Some of Apple’s descriptions
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 doc‐
umentation that every professional iOS developer should read.
For starters, I suggest that you have a look at the iOS Human Interface Guidelines for
all iOS devices. This document will tell you everything you need to know about devel‐
oping engaging and intuitive user interfaces for all iOS devices. Every iOS programmer
should read this document. In fact, I believe this should be required reading for the
product design and development teams of any company that develops iOS applications.
xvi | Preface
23. I also suggest that you skim through the “iOS App Programming Guide” in the iOS
Developer Library for some tips and advice on how to make great iOS applications.
iOS 7 brings with itself quite a lot of changes to how UI components appear on the
screen.Wewilltalkatgreatlengthaboutthesechangesandhowyou,astheprogrammer,
can use the latest APIs to create great-looking apps for iOS 7. However, I would like to
suggest that you have a look at the iOS 7 UI Transition Guide provided by Apple, which
outlines all the UI changes that have now been made to the latest version of the SDK.
One of the things you will notice when reading Chapter 16 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”.
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”.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, databases, data types, environment variables,
statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter‐
mined by context.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
Preface | xvii
24. Using Code Examples
Supplemental material (code examples, exercises, etc.) is available for download at
https://github.com/oreillymedia/iOS7_programming_cookbook.
This book is here to help you get your job done. In general, if example code is offered
with this book, you may use it 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 ex‐
ample 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 7 Programming Cookbook
by Vandad Nahavandipoor (O’Reilly). Copyright 2014 Vandad Nahavandipoor,
978-1-4493-7242-2.”
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.
Safari® Books Online
Safari Books Online (www.safaribooksonline.com) is an on-
demand digital library that delivers expert content in both book and
video form from the world’s leading authors in technology and busi‐
ness.
Technology professionals, software developers, web designers, and business and crea‐
tive professionals use Safari Books Online as their primary resource for research, prob‐
lem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organi‐
zations, government agencies, and individuals. Subscribers have access to thousands of
books, training videos, and prepublication manuscripts in one fully searchable database
from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Pro‐
fessional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John
Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT
Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technol‐
ogy, and dozens more. For more information about Safari Books Online, please visit us
online.
xviii | Preface
25. How to Contact Us
Please address comments and questions concerning this book to the publisher:
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://oreil.ly/iOS7-Programming-Cookbook.
To comment or ask technical questions about this book, send email to bookques
tions@oreilly.com.
For more information about our books, courses, conferences, and news, see our website
at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
Andy Oram, my lovely editor, has again done an amazing job going through all the
changes that I made in this edition of the book. In fact, the whole book is updated in
this edition, and all example codes and screenshots have also been updated. I’d like to
alsothankKrzysztofGrobelnyandKrzysztofGutowski,mygreatfriendsandcolleagues,
for tech-reviewing this book. Without their help, this book wouldn’t be in your hands.
I’d like to say thank you to Rachel Roumeliotis, for supporting me and Andy, among all
the other admin work that she did for us behind the scenes. Rachel, you may be quiet,
but we’d have to be blind not to notice your hard work in the background. Also, Meghan
Connolly of O’Reilly has been a fantastic sport, listening to my nagging about paper‐
work, and she has been absolute bliss to work with. A thank-you goes to Jessica Hosman
for helping us a great deal with Git issues. Even though I didn’t believe the simple sol‐
utions that she suggested to me would work, they did, and I looked like a fool.
Last but not least, thank you to Alina Rizzoni, Bruno Packham, and Thomas Packham
for being real friends. I feel blessed to know them, and I appreciate their help and
support.
Preface | xix
27. CHAPTER 1
Implementing Controllers and Views
1.0. Introduction
iOS 7 has introduced a lot of new features to users, as well as tons of new APIs for us
programmers to use and play with. You probably already know that the user interface
has drastically changed in iOS 7. This user interface had stayed intact all the way from
the first version of iOS till now, and because of this, many apps were coded on the
assumption that this user interface would not ever change. Graphic designers are now
faced with the challenge of creating the user interface and thinking about the user ex‐
perience in a way that makes it great for both pre- and post-iOS 7 user interfaces (UIs).
In order to write apps for iOS 7, you need to know some of the basics of the Objective-
C programming language that we will use throughout this book. Objective-C, as its
name implies, is based on C with extensions that allow it to make use of objects. Objects
and classes are fundamental in object-oriented programming (OOP) languages such as
Objective-C, Java, C++, and many others. In Objective-C, like any other object-oriented
language(OOL),youhavenotonlyaccesstoobjects,butalsotoprimitives.Forinstance,
the number –20 (minus twenty) can be expressed simply as a primitive in this way:
NSInteger myNumber = -20;
This simple line of code will define a variable named myNumber with the data type of
NSInteger and sets its value to 20. This is how we define variables in Objective-C. A
variable is a simple assignment of a name to a location in memory. In this case, when
we set 20 as the value of the myNumber variable, we are telling the machine that will
eventually run this piece of code to put the aforementioned value in a memory location
that belongs to the variable myNumber.
All iOS applications essentially use the model-view-controller (MVC) architecture.
Model, view, and controller are the three main components of an iOS application from
an architectural perspective.
1
28. The model is the brain of the application. It does the calculations and creates a virtual
world for itself that can live without the views and controllers. In other words, think of
a model as a virtual copy of your application, without a face!
Aviewisthewindowthroughwhichyourusersinteractwithyourapplication.Itdisplays
what’s inside the model most of the time, but in addition to that, it accepts users’ inter‐
actions. Any interaction between the user and your application is sent to a view, which
then can be captured by a view controller and sent to the model.
The controller in iOS programming usually refers to the view controllers I just men‐
tioned. Think of a view controller as a bridge between the model and your views. This
controller interprets what is happening on one side and uses that information to alter
the other side as needed. For instance, if the user changes some field in a view, the
controller makes sure the model changes in response. And if the model gets new data,
the controller tells the view to reflect it.
In this chapter, you will learn how to create the structure of an iOS application and how
to use views and view controllers to create intuitive applications.
In this chapter, for most of the user interface (UI) components that we
create, we are using a Single View Application template in Xcode. To
reproduce the examples, follow the instructions in “Creating and Run‐
ning Our First iOS App” on page 2. Make sure that your app is uni‐
versal, as opposed to an iPhone or iPad app. A universal app can run
on both iPhone and iPad.
Creating and Running Our First iOS App
Before we dive any deeper into the features of Objective-C, we should have a brief look
at how to create a simple iOS app in Xcode. Xcode is Apple’s IDE (integrated develop‐
ment environment) that allows you to create, build, and run your apps on iOS Simulator
and even on real iOS devices. We will talk more about Xcode and its features as we go
along, but for now let’s focus on creating and running a simple iOS app. I assume that
you’ve already downloaded Xcode into your computer from the Mac App Store. Once
that step is taken care of, please follow these steps to create and run a simple iOS app:
1. Open Xcode if it’s not already open.
2. From the File menu, choose New Project...
3. In the New Project window that appears, on the lefthand side under the iOS cate‐
gory, choose Application and then on the righthand side choose Single View Ap‐
plication. Then press the Next button.
4. On the next screen, for the Product Name, enter a name that makes sense for you.
For instance, you can set the name of your product as My First iOS App. In the
2 | Chapter 1: Implementing Controllers and Views
29. Organization Name section, enter your company’s name, or if you don’t have a
company, enter anything else that makes sense to you. The organization name is
quite an important piece of information that you can enter here, but for now, you
don’t have to worry about it too much. For the Company Identifier field, enter
com.mycompany. If you really do own a company or you are creating this app for a
company that you work with, replace mycompany with the actual name of the com‐
pany in question. If you are just experimenting with development on your own,
invent a name. For the Devices section, choose Universal.
5. Onceyouaredonesettingtheaforementionedvalues,simplypresstheNextbutton.
6. You are now being asked by Xcode to save your project to a suitable place. Choose
a suitable folder for your project and press the Create button.
7. Assoonasyourprojectiscreated,youarereadytobuildandrunit.However,before
you begin, make sure that you’ve unplugged all your iOS devices from your com‐
puter. The reason behind this is that once an iOS device is plugged in, by default,
Xcode will attempt to build and run your project on the device, causing some issues
with provisioning profiles (which we haven’t talked about yet). So unplug your iOS
devices and then press the big Run button on the top-lefthand corner of Xcode. If
you cannot find the Run button, go to the Product menu and select the Run menu
item.
Voilà! Your first iOS app is running in iOS Simulator now. Even though the app is not
exactly impressive, simply displaying a white screen in the simulator, this is just the first
step toward our bigger goal of mastering the iOS SDK, so hold on tight as we embark
on this journey together.
Defining and Understanding Variables
All modern programming languages, including Objective-C, have the concept of vari‐
ables. Variables are simple aliases to locations in the memory. Every variable can have
the following properties:
1. A data type, which is either a primitive, such as an integer, or an object
2. A name
3. A value
You don’t always have to set a value for a variable, but you need to specify its type and
its name. Here are a few data types that you will need to know about when writing any
typical iOS app:
1.0. Introduction | 3
30. Mutable Versus Immutable
If a data type is mutable, you can change if after it is initialized. For
instance, you can change one of the values in a mutable array, or add
or remove values. In contrast, you must provide the values to an im‐
mutable data type when you initialize it, and cannot add to them,
remove them, or change them later. Immutable types are useful be‐
cause they are more efficient, and because they can prevent errors when
the values are meant to stay the same throughout the life of the data.
NSInteger and NSUInteger
Variables of this type can hold integral values such as 10, 20, etc. The NSInteger
type allows negative values as well as positive ones, but the NSUInteger data type
is the Unsigned type, hence the U in its name. Remember, the phrase unsigned in
programming languages in the context of numbers always means that the number
must not be negative. Only a signed data type can hold negative numbers.
CGFloat
Holds floating point variables with decimal points, such as 1.31 or 2.40.
NSString
Allows you to store strings of characters. We will see examples of this later.
NSNumber
Allows you to store numbers as objects.
id
Variables of type id can point to any object of any type. These are called untyped
objects. Whenever you want to pass an object from one place to another but do not
wish to specify its type for whatever reason, you can take advantage of this data
type.
NSDictionary and NSMutableDictionary
These are immutable and mutable variants of hash tables. A hash table allows you
to store a key and to associate a value to that key, such as a key named phone_num
that has the value 05552487700. Read the values by referring to the keys associated
with them.
NSArray and NSMutableArray
Immutableandmutablearraysofobjects.Anarrayisanorderedcollectionofitems.
For instance, you may have 10 string objects that you want to store in memory. An
array could be a good place for that.
NSSet, NSMutableSet, NSOrderedSet, NSMutableOrderedSet
Sets are like arrays in that they can hold series of objects, but they differ from arrays
in that they contain only unique objects. Arrays can hold the same object multiple
4 | Chapter 1: Implementing Controllers and Views
31. times, but a set can contain only one instance of an object. I encourage you to learn
the difference between arrays and sets and use them properly.
NSData and NSMutableData
Immutable and mutable containers for any data. These data types are perfect when
you want to read the contents of a file, for instance, into memory.
Some of the data types that we talked about are primitive, and some are classes. You’ll
just have to memorize which is which. For instance, NSInteger is a primitive data type,
but NSString is a class, so objects can be instantiated of it. Objective-C, like C and C++,
has the concept of pointers. A pointer is a data type that stores the memory address
where the real data is stored. You should know by now that pointers to classes are
denoted using an asterisk sign:
NSString *myString = @"Objective-C is great!";
Thus, when you want to assign a string to a variable of type NSString in Objective-C,
you simply have to store the data into a pointer of type NSString *. However, if you are
about to store a floating point value into a variable, you wouldn’t specify it as a pointer
since the data type for that variable is not a class:
/* Set the myFloat variable to PI */
CGFloat myFloat = M_PI;
If you wanted to have a pointer to that floating point variable, you could do so as follows:
/* Set the myFloat variable to PI */
CGFloat myFloat = M_PI;
/* Create a pointer variable that points to the myFloat variable */
CGFloat *pointerFloat = &myFloat;
Getting data from the original float is a simple dereference (myFloat), whereas getting
the value of through the pointer requires the use of the asterisk (*pointerFloat). The
pointer can be useful in some situations, such as when you call a function that sets the
value of a floating-point argument and you want to retrieve the new value after the
function returns.
Going back to classes, we probably have to talk a bit more about classes before things
get lost in translation, so let’s do that next.
Creating and Taking Advantage of Classes
A class is a data structure that can have methods, instance variables, and properties,
along with many other features, but for now we are just going to talk about the basics.
Every class has to follow these rules:
1.0. Introduction | 5
32. • The class has to be derived from a superclass, apart from a few exceptions such as
NSObject and NSProxy classes, which are root classes. Root classes do not have a
superclass.
• It has to have a name that conforms to Cocoa’s naming convention for methods.
• It has to have an interface file that defines the interface of the class.
• It has to have an implementation where you implement the features that you have
promised to deliver in the interface of the class.
NSObject is the root class from which almost every other class is inherited. For this
example, we are going to add a class, named Person, to the project we created in “Cre‐
ating and Running Our First iOS App” on page 2. We are going to then add two prop‐
erties to this class, named firstName and lastName, of type NSString. Follow these
steps to create and add the Person class to your project:
1. In Xcode, while your project is open and in front of you, from the File menu, choose
New → File...
2. On the lefthand side, ensure that under the iOS main section you have chosen the
Cocoa Touch category. Once done, select the Objective-C Class item and press the
Next button.
3. In the Class section, enter Person.
4. In the “Subclass of” section, enter NSObject.
5. Once done, press the Next button, at which point Xcode will ask where you would
like to save this file. Simply save the new class into the folder where you have placed
your project and its files. This is the default selection. Then press the Create button,
and you are done.
You now have two files added to your project: Person.h and Person.m. The former is the
interface and the latter is the implementation file for your Person class. In Objective-
C, .h files are headers, where you define the interface of each class, and .m files are
implementation files where you write the actual implementation of the class.
Now let’s go into the header file of our Person class and define two properties for the
class, of type NSString:
@interface Person : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@end
Just like a variable, definition of properties has its own format, in this particular order:
6 | Chapter 1: Implementing Controllers and Views
33. 1. The definition of the property has to start with the @property keyword.
2. You then need to specify the qualifiers of the property. nonatomic properties are
not thread-safe. We will talk about thread safety in Chapter 16. You can also specify
assign, copy, weak, strong, or unsafe_unretained as the property qualifiers. We
will read more about these soon too.
3. You then have to specify the data type of the property, such as NSInteger or
NSString.
4. Last but not least, you have to specify a name for the property. The name of the
property has to follow the Apple guidelines.
We said that properties can have various qualifiers. Here are the important qualifiers
that you need to know about:
strong
Properties of this type will be retained by the runtime. These can only be instances
of classes. In other words, you cannot retain a value into a property of type strong
if the value is a primitive. You can retain objects, but not primitives.
copy
The same as strong, but when you assign to properties of this type, the runtime
will make a copy of the object on the right side of the assignment. The object on
the righthand side of the assignment must conform to the NSCopying or NSMutable
Copying protocol.
assign
Objects or primitive values that are set as the value of a property of type assign will
not be copied or retained by that property. For primitive properties, this qualifier
will create a memory address where you can put the primitive data. For objects,
properties of this type will simply point to the object on the righthand side of the
equation.
unsafe_unretained
The same as the assign qualifier.
weak
The same as the assign qualifier with one big difference. In the case of objects,
when the object that is assigned to a property of this type is released from memory,
the runtime will automatically set the value of this property to nil.
We now have a Person class with two properties: firstName and lastName. Let’s go back
to our app delegate’s implementation (AppDelegate.m) file and instantiate an object of
type Person:
#import "AppDelegate.h"
#import "Person.h"
1.0. Introduction | 7
34. @implementation AppDelegate
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
Person *person = [[Person alloc] init];
person.firstName = @"Steve";
person.lastName = @"Jobs";
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
We are allocating and initializing our instance of the Person class in this example. You
may not know what that means yet, but continue to the “Adding Functionality to Classes
with Methods” on page 8 section and you will find out.
Adding Functionality to Classes with Methods
Methods are building blocks of classes. For instance, a class named Person can have
logical functionalities such as walk, breathe, eat, and drink. These functionalities are
usually encapsulated in methods.
A method can take parameters, which are variables that the caller passes when calling
themethodandarevisibleonlytothemethod.Forinstance,inasimpleworld,wewould
have a walk method for our Person class. However, if you want, you can add a parameter
or argument to the method and name it walkingSpeed of type CGFloat, so that when
another programmer calls that method on your class, she can specify the speed at which
the person has to walk. You, as the programmer of that class, would then write the
appropriate code for your class to handle different speeds of walking. Don’t worry if this
all sounds like too much, but have a look at the following example, where I have added
a method to the implementation file we created in “Creating and Taking Advantage of
Classes” on page 5 for our Person class:
#import "Person.h"
@implementation Person
- (void) walkAtKilometersPerHour:(CGFloat)paramSpeedKilometersPerHour{
/* Write the code for this method here */
}
- (void) runAt10KilometersPerHour{
/* Call the walk method in our own class and pass the value of 10 */
8 | Chapter 1: Implementing Controllers and Views
36. The text on this page is estimated to be only 28.64%
accurate
454 INDEX Detroit and Milwaukee Railroad, 216 Discounts
on sale of Pere Marquette bonds, 392-398 Dividends paid out of
capital, 299, 369, 394, 409, 432 Eastern Equipment Company,
organization, 291; debt of Pere Marquette to, 338, 393, 395 East
Saginaw and St. Clair Railroad, purchased by Pere Marquette, 220
Erb, Newman, 346 Erie Railroad, purchase of Cincinnati, Hamilton
and Dayton by, 304, 371; reasons for purchase, 304-306; relieved of
purchase by J. P. Morgan and Company, 307-309; reasons for J. P.
Morgan and Company's action, 309-316 Flint, as lumber center in
1863, 215-216 Flint and Holly Railroad, origin and history of, 216;
record of operations, 234 Flint and Pere Marquette Railway, origin,
215; extensions and connections, 215, 216, 218, 219, 220, 221,
238; earnings, 215, 218, 222; sale of lands, 217, 218, 219; business
activities along, 219; mileage, 221; lack of traffic, 221-222; effects of
business depression, 222; receivership, 222; financial history, 231
Flint and Pere Marquette Railroad, origin, 223; capitalization and
bonded indebtedness, 223, 385386, 391-392; earnings, 223, 224,
225, 227, 228, 229, 267 ; extensions and connections, 223, 224,
225, 227, 228, 229; lake traffic, 224, 225, 227 ; traffic in forest
products, 226; entrance into Detroit, 228; land sales, 230, 233;
passenger traffic, 230; financial history, 232; consolidation, 263-264,
367; responsibility on equipment bonds, 271 Flint River Railroad,
purchased by Pere Marquette, 220; record of operations, 234
Foreclosure sales, 252, 255, 297 Fort Street Union Depot Company,
228, 353 Fraudulent sales of railroads, 293294, 296-297, 304-316,
321, 370371, 399 Freight, number of tons carried one mile per mile
of road, 422-423, 426; average distance haul of one ton of, 424,
426; average amount received from each ton of, 425-426; average
receipts per ton per mile from, 426-427, 42.8; earnings from, per
mile of road, 427, 429; earnings from, per train mile, 427, 430, 432;
character of, 440, 443-445 Freight cars, deferred maintenance on,
408-409, 411, 432 Fruit and sugar beets, tonnage of, 355 Funded
indebtedness, relation of increase of, to Pere Marquette deficit, 337-
37. 338 ; abnormal size of Pere Marquette, 372 Grand Haven Railroad,
origin, 236; capitalization, 236; earnings, 236237; character of
tonnage, 237; consolidation, 237, 244 Grand Rapids, entrance of
Chicago and Michigan Lake Shore Railroad into, 238; construction
north of, 242 Grand Rapids, Belding and Saginaw Railroad, leased to
Pere Marquette, 269; issue of Pere Marquette bonds to, 393
38. The text on this page is estimated to be only 28.36%
accurate
INDEX 455 Grand Rapids and Holland Railroad, 243 Grand
Rapids and Indiana Railway, 219; competition of Pere Marquette
with, 245, 355; connections with, 251, 257; advantage over Pere
Marquette, 358; charges to operating expenses for ties and rails,
405-406, locomotives, 405, 407-408, passenger cars, 408, 410,
freight cars, 408409, 411, way and structures, 409, 412-413;
operating ratio, 437, 441; passengers and passenger earnings, see
Passengers; freight and freight earnings, see Freight; operating
revenues, see Operating Revenues; operating expenses, see
Operating 'Expenses Grand Rapids, Kalkaska and South Eastern
Railroad, 246; leased by Pere Marquette, 269; purchased by Pere
Marquette, 285-286, 292293; original cost, 287; loss to Pere
Marquette through purchase, 287-288; unprofitable location of, 427;
portion of, discontinued, 427 Grand Rapids, Lansing, and Detroit
Railroad, 258 Grand Rapids, Lansing and Northern Railroad,
consolidation, 260 Grand Rapids, Newaygo and Lake Shore Railroad,
242; cost, 242; capitalization, 242; financial history, 242-243 Grand
Trunk Railroad, organization of the Toledo Terminal Railroad by, 324;
competition of, in Toledo, 325-326; competitor of Pere Marquette,
355 Group III of railroads, passengers and passenger earnings, see
Passengers; freight and freight earnings, see Freight Harding,
Russell, 299-301 Haskell and Barker equipment bonds, 393 Hire of
Equipment account, relation of, to Pere Marquette deficit, 336-337
Hollins, H. B., and Company, sale of Toledo Railway and Terminal
Company stock through, 296; relation of, to sale of Cincinnati,
Hamilton and Dayton control, 304, 320 Holly, Wayne and Monroe
Railroad, opening of, 220; bonded indebtedness, 391 Howell and
Lansing Railroad, 250 Huron and Western Railroad, purchased by
Pere Marquette, 289 Indiana and Michigan Railroad, 244 Industries,
slowness of growth of, 227, 230 Interstate Commerce Commission,
226; investigation of Pere Marquette by, 211; difficulties encountered
in investigation, 315; excellent work of, 381; attempts correction of
Pere Marquette accounting methods, 388-389 Investors,
39. reorganizations cause losses to, 207 Ionia and Lansing Railroad,
250251; bonded indebtedness, 392 Ionia and Stanton Railroad, 251
Jackson, Lansing and Saginaw Railroad, 253 Kalamazoo, attempt of
Pere Marquette to enter, 280, 281 Kalamazoo, Lake Shore and
Chicago Railroad, 282 Lake Erie and Detroit River Railway, purchased
by Pere
40. The text on this page is estimated to be only 28.33%
accurate
456 INDEX Marquette, 275, 393; profitable investment,
276-277 Lake Erie International Equipment Company, 395 Lake
Michigan, traffic across, 224, 226, 228; change in character of traffic
across, 229; value of traffic across, 230 Lake Shore and Michigan
Southern Railroad, purchase of trackage rights from, 282-283; use of
Toledo terminals, 295; organization of Toledo Terminal Railroad by,
324; aids Pere Marquette enter Chicago, 349 Lake Shore Railroad of
Western Michigan, 243 Land grants, to Flint and Pere Marquette
Railway, 217, 221; to Flint and Pere Marquette Railroad, 230,232
Land sales, 217, 218, 219, 221,230, 233 Locomotives, deferred
maintenance on, 405, 407-408, 432; need of heavier, 427 London
and Port Stanley Railroad, controlled by Pere Marquette, 276 Lowell
and Hastings Railroad, 262 Ludington, entrance of Flint and Pere
Marquette into, 221; erection of grain elevator, 222; center of
through traffic, 351 Lumber, industry important until recently, 208;
early dependence of Flint and Pere Marquette Railway on, 209-210;
statistics of production, 218; effects of business depression on, 224;
manufacture of, in Saginaw River Valley, 225; lake traffic in, 225;
decline of tonnage in forest products and, 226, 237, 354, 414, 426,
440, 443-445; importance of, in local traffic, 230, 235, 239, 246,
252, 253, 254, 258-259, 366; disappearance of, industry, 230;
fluctuating character of, traffic, 236, 240; lake traffic an offset to
decreased tonnage in, 279 Manistee Railroad, 234 Manistique,
Marquette and Northern Railroad, leased by Pere Marquette, 291
Manitowoc, nature of, as Pere Marquette terminal, 351, 353
Manufactures and merchandise tonnage, increasing importance of,
237, 239, 246; Pere Marquette tonnage in, 355, 426, 440, 443-445
Marquette and Bessemer Dock and Navigation Company,
organization, 289 Marquette Construction Company, 402-403
Marquette Equipment Company, organization, 270; bond issues to,
392 Michigan Central Railroad, competition with Pere Marquette,
245, 325-326, 355; aids Pere Marquette reach Buffalo, 277, 278,
282, 350-351; thwarts Pere Marquette shortening main line, 280;
41. organization of Toledo Terminal Railroad by, 324; Pere Marquette at
a disadvantage compared with, 358; charges to operating, expenses
for ties and rails, 405-406, locomotives, 405, 407-408, passenger
cars, 408, 410, freight cars, 408-409, 411, way and structures, 409,
412-413; extent of through traffic, 433; passengers and passenger
earnings, see Passengers; freight and freight earnings, see Freight;
operating revenues, see Operating
42. The text on this page is estimated to be only 28.33%
accurate
1XDKX 457 Revenues; operating expenses, see Operating
Expenses Michigan Equipment Company, bonded indebtedness, 392
Michigan Lake Shore Railroad, origin, 235; cost, 235; earnings, 235,
236; dependence on lumber traffic, 236, 239; receivership, 236;
extensions, 238 Michigan Legislature, nature of Pere Marquette
investigation by, 381 Michigan Railroad Commission, reports of, 231;
permits abandonment of branch line, 288; first Pere Marquette
reorganization approved by, 330-333; sanctions further issues of
bonds, 343, 346, 374; refuses further issues of bonds, 346-347;
opinion of, regarding second Pere Marquette. reorganization, 361-
364; premature endorsement of Pere Marquette reorganization by,
372-373; nature of the Orders and Opinions of, 381 Milwaukee,
nature of Pere Marquette terminals at, 351-352 Milwaukee, Benton
Harbor and Columbus Railroad, purchased by Pere Marquette, 279,
392; financial character, 282 Mines, tonnage in products of, 440,
443-445 Monroe and Toledo Railway, 229 Montague, Pentwater and
Manistee Railroad, 243 Morgan, J. P. and Company, connection of,
with sale of Cincinnati, Hamilton and Dayton control, 304-316, 371;
sale of this control by, 334; purchases Pere Marquette control, 341;
financing of Pere Marquette by, 343; refuses to further finance Pere
Marquette, 345, 375; attitude toward reorganization of Pere
Marquette, 372; schemes employed to prevent foreclosure, 374,
380; sale of Pere Marquette bonds through, 396, 398 Muskegon,
entrance of Chicago and Michigan Lake Shore Railroad into, 238
Muskegon and Big Rapids Railroad, 244 Muskegon and Ferrysburg
Railroad, 235 Nathaniel Thayer Estate, sale of Pere Marquette bonds
to, 272 New York Central Railroad, Pere Marquette purchases
trackage rights from, 278; release of Pere Marquette from trackage
rights contract,. 327; aids Pere Marquette enter Buffalo, 350-351
Operating Expenses, per train mile, 432-433, 434; low, due to
deferred maintenance, 432; compared with operating revenues, 433,
435; per mile of road, 433, 437, 438; per mile of road compared
with operating revenues per mile of road, 437, 439 Operation ratio,
43. 437, 441 Operating revenues (gross), per train mile, 431-432 ; per
train mile compared with operating expenses per train mile, 433,
435; per mile of road, 433, 436; per mile of road compared with
operating expenses per mile of road, 437, 439 Operating Revenues
(net), per mile of road, 437, 440, 442 Panics, see Business
Depressions Passengers, number of, carried one mile per mile of
road, 414-415; average distance carried, 414,
44. The text on this page is estimated to be only 28.18%
accurate
458 INDEX 416, 418; lack of, on branch lines, 418; average
amount received from, 417-418; average receipts per passenger per
mile, 418-419; passenger earnings per mile of road, 418, 420, 422;
passenger earnings per train mile, 421-422; value of a larger
proportion of passenger traffic, 240 Passenger cars, deferred
maintenance on, 408, 410, 432 Pennsylvania Railroad, purchase of
trackage rights from, 284-285; organization of the Toledo Terminal
Railroad by, 324 Pere Marquette Railroad, why chosen as subject of
special study, 208; relation between the lumber industry and, 208;
independent character of, 209, 210; an example of the results of a
laissez faire policy, 209; peculiar characteristics of, 209 ; difficulties
in studying, 210; slow growth of, 215; origin, 215, 263, 268; bonded
indebtedness, 263; capitalization, 264, 269; leases, 269, 291, 325;
increases in the funded debt, 270, 271, 272, 273, 275, 276, 280,
286, 291, 296, 299, 301, 343, 346, 369; purchases, 270, 275, 279,
285-290, 292, 296; organization of Marquette Equipment Company,
270, Eastern Equipment Company, 291; new method of state
taxation, 272; F. H. Prince secures control, 273; trackage rights, 277,
278, "282-285; entrance into Buffalo, 279, Chicago, 284; extensions.
288, 290; new coal supply, 290; syndicate control, 293-303; rental of
Toledo terminals, 295; worthless railroad purchases, 293-297; leased
to Cincinnati, Hamilton and Dayton Railroad, 298-299; ruined by
syndicate, 303; Erie Railroad secures control, 307; first receivership,
316, 371' United States Mortgage and Trust Company secures
control, 317; increased taxes, 318; two-cent fares, 318-319; loss
from Chicago, Cincinnati and Louisville Railroad purchase, 319-320;
reorganization difficulties, 319-333; syndicate treatment, 321; legal
settlement with Toledo Railway and Terminal Company, 321-324;
organization of Toledo Terminal Railroad Company, 324; termination
of lease, 324-325; reasons for lease, 325-326; time extensions on
equipment payments, 326-327; cancellation of Canadian Southern
rentals, 32.6; difficulties in securing release from control of
Cincinnati, Hamilton and Dayton, 327-328; plan for raising monies,
45. 328-329; defects in plan reorganization, 332-333; release of stock
from refunding mortgage, 334; Baltimore and Ohio purchases stock
control, 334; financial difficulties, 335-340; poor credit, 338-339; J.
P. Morgan acquires stock control, 341; financial difficulties, 342; J. P.
Morgan and Company finances, 343; uses made of loan, 344; cause
of deficit, 344-345; J. P. Morgan and Company refuses further
financial aid, 345; Boston stockholders lend funds, 346; second
receivership, 347, 376; financial history, 347-348; present physical
system, 349-353 -r means of entering Chicago, 349-350; means of
entering Buffalo, 349-351; roundabout nature of trunk line, 351,
353; an originating road, 354; branch lines, 356; see Branch
46. The text on this page is estimated to be only 28.01%
accurate
INDEX 459 Lines; plan of second reorganization, 359-364,
377; wrecking of, 368-371; financial difficulties, 371373; financial
collapse, 373-375; physical collapse, 375-376; difficulties of
reorganization, 377; reasons for failure of, 381; analysis of capital
stock, 385-390; analysis of bond account, 39 1-398 ; bad financial
policy of, 397; deferred maintenance, 405-414; operating ratio, 437,
441; freight and freight earnings, see Freight; passengers and
passenger earnings, see Passengers; competition of other roads
with, see Competition; operating revenues, see Operating Revenues;
operating expenses, see Operating Expenses Pere Marquette
Railway, origin, 364, 377 Pere Marquette Railroad of Indiana,
organization, 282; capitalization, 283; consolidated with Pere
Marquette, 330, 387; issue of Pere Marquette bonds to, 393;
financial affairs, 401-404 Pere Marquette Transportation Company,
bonds of, 391-392 Pittsburg, Fort Wayne and Chicago Railroad, aids
Pere Marquette enter Chicago, 349 Port Huron and North Western
Railway, 227; financial history, 232 Prince, F. H., Pere Marquette
control purchased by, 273, 368; administration of, 275-292; member
of syndicate, 293, 370; sells Pere Marquette control to syndicate,
294; Pere Marquette funded debt increased by, 299; retires from
presidency of Pere Marquette, 300; reasons for expansion policy of,
368; policy of undermaintenance practiced by, 368, 373, 375, 405,
437 ; stock bonus received by, 387 Public, dissatisfied with service of
public carriers, 207; loss to, because of syndicate policies, 370;
wronged by inefficient reorganization of Pere Marquette, 373-374,
375; laxity of, in regulating security issues, 378; must be careful to
whom it gives its trust, 379; necessity of, control of railroads
indicated by this study, 378-379 Pullman Company equipment notes,
394, 398 Rails and ties, deferred maintenance on, 405, 432;
extensive laying of, 437 Rates, relation to traffic, 221; "low and
unremunerative," 224; efforts to increase traffic by reduction of,
226-227; failure of Pere Marquette not due to low, 380
Receiverships, statistics of, 207; effects of, 207; Flint and Pere
47. Marquette Railway, 222; Michigan Lake Shore Railroad, 236; Chicago
and Michigan Lake Shore Railroad, 240; Detroit, Lansing and Lake
Michigan Railroad, 252; Detroit, Lansing and Northern Railroad, 259;
Cincinnati, Hamilton and Dayton Railroad, 316; Pere Marquette
Railroad, 316, 347; Chicago, Cincinnati and Louisville Railroad, 321;
inefficiency of first Pere Marquette receivership, 345 Reorganizations,
Flint and Pere Marquette Railway, 223; capitalizing deficits in, 234,
243, 244, 269; Michigan Lake Shore Railroad, 236; Chicago and
Michigan Lake Shore Railroad, 240; Chicago and West Michigan Rail
48. The text on this page is estimated to be only 28.24%
accurate
460 INDEX way, 244; methods of, 248-249; Detroit, Lansing
and Lake Michigan, 252; Chicago, Saginaw and Canada Railroad,
255; Detroit, Lansing and Northern, 259; Toledo Railway and
Terminal Company, 323-324; Pere Marquette Railroad, 316, 319-333,
347, 376; Cincinnati, Hamilton and Dayton Railroad, 334; defects of
Pere Marquette reorganization, 343-344; plan of Pere Marquette
second reorganization, 359-364, 376-377; nature of most, 366;
reason for Pere Marquette, 370; pretext for stock inflation, 372;
inefficient, 372373; commissions too readily pass on plans of, 380;
necessity for cutting down fixed charges in Pere Marquette, 437
Robert Winthrop and Company, Pere Marquette bonds sold to, 271,
272, 392, 394, 395 Saginaw and Grand Rapids Railroad, 254;
consolidation, 260 Saginaw, Tuscola and Huron Railroad, purchased
by Pere Marquette, 270 Saginaw and -Western Railroad, 257 ;
consolidations, 260 Saginaw Valley and St. Louis Railroad, origin,
253; capitalization, 253; character of traffic, 253; financial history,
254, 260-261; transfer of control of, 254; extensions, 254;
consolidation, 260 Salt, early tonnage, 210; statistics of production,
218; lake traffic in, 225 Sarnia, Petrolia and St. Thomas Railway,
worthless nature of stock, 339 Sikes, C. L., 401 Soo Line, basis of
connection with Pere Marquette at Manitowoc, 353; branch lines,
356 Source materials for this study, 211212 South Haven and
Eastern Railroad, purchased by Pere Marquette, 279, 392; financial
history, 281; leased, 282 Stevens, F. W., 322, 400 Stock, excessive
issues, 265, 368, 372, 403 Stockholders, necessity of greater
vigilance by minority, 379; of banking houses should understand
banks affairs, 379 Supplies and materials, relation of increased cost
of, to Pere Marquette deficit, 336, 373 Suspension Bridge, important
terminal for Pere Marquette, 350351 Syndicate, Pere Marquette
control acquired by, 293 ; composition of, 293, 370; real
administrator of, 300; ruin of Pere Marquette and Cincinnati,
Hamilton and Dayton by, 303, 368; explanation of sale of Cincinnati,
Hamilton and Dayton stock by, 310-316, 371; stockholders' protests
49. silenced by, 320 Taxation, new method of state, 272, 318 "Thumb"
district, railroad activities in, 227; unprofitable nature of, 427 Toledo,
entrance of Flint and Pere Marquette Railroad into, 229; nature of
Pere Marquette entrance into, 353 Toledo Railway and Terminal
Company, represented on syndicate, 293; rental of terminals, 295;
purchased by Pere Marquette, 296, 395; Pere Marquette ad
50. The text on this page is estimated to be only 28.42%
accurate
INDEX 461 vanced expenses of,* 297; foreclosure sale, 297
; Pere Marquette • received settlement from, 321323;
reorganization, 323-324, 331; Pere Marquette enters Toledo by
means of, 353; worthless nature of stock, 399 Toledo and South
Haven Railroad,' 281 Toledo Terminal Railroad Company,
organization, 324 Tonnage, character of 440, 443-445 Traffic, Pere
Marquette cut off from through, 209; importance of through, '230,
240, 247, 259, 354, 355, 358, 367, 422, 433, 440; across lake, 224,
228; increasing through, 275, 279, 354, 369, 445; extent of through,
351, 378; no through passenger, 414 Two-cent fare law, 318-319,
332, 373, 418 United States Express Company, represented on
syndicate, 293; profits from syndicate membership, 298 United
States railroads, passengers and passenger earnings, see
Passengers; freight and freight earnings, see Freight Wabash
Railroad, Pere Marquette enters Detroit in cooperation with, 353;
competitor of Pere Marquette, Pere Marquette at a disadvantage
compared with, 358 Wages, relation of increased, to Pere Marquette
deficit, 335, 373 Water competition, Pere Marquette subject to, 210;
extent of in lumber, 225; extent of, in salt, 225; efforts to meet, 226;
effects of, 227, 247 Way and structures, deferred maintenance on,
409, 412-413, 432; extensive improvement of, 437 Weather
conditions, affecting lake traffic, 224, 225, 228; affecting rail traffic,
344 Western Equipment Company, bonds of, 392, 395 White River
Railroad, 244; consolidation, 245 Zimmerman, Eugene, 299-300;
inefficiency of the administration of, 301, 303, 405, 437
60. 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!
ebookname.com