iOS 7 App DevelopmentEssentials
Table of Contents
1. Start Here 1
1.1 For New iOS Developers 2
1.2 For iOS 6 Developers 2
1.3 Source Code Download 3
1.4 Feedback 3
1.5 Errata 3
2. Joining the Apple iOS Developer Program 5
2.1 Registered Apple Developer 5
2.2 Downloading Xcode and the iOS 7 SDK 5
2.3 iOS Developer Program 5
2.4 When to Enroll in the iOS Developer Program? 6
2.5 Enrolling in the iOS Developer Program 6
2.6 Summary 8
3. Installing Xcode 5 and the iOS 7 SDK 9
3.1 Identifying if you have an Intel or PowerPC based Mac 9
3.2 Installing Xcode 5 and the iOS 7 SDK 10
3.3 Starting Xcode 10
4. Creating a Simple iOS 7 App 13
4.1 Starting Xcode 5 13
4.2 Creating the iOS App User Interface 18
4.3 Changing Component Properties 20
4.4 Adding Objects to the User Interface 20
4.5 Building and Running an iOS 7 App in Xcode 5 22
4.6 Dealing with Build Errors 23
4.7 Testing Different Screen Sizes 23
4.8 Testing User Interface Appearance in Different iOS Versions 23
4.9 Monitoring Application Performance 25
4.10 Summary 26
5. iOS 7 Architecture and SDK Frameworks 27
5.1 iPhone OS becomes iOS 27
5.2 An Overview of the iOS 7 Architecture 27
5.3 The Cocoa Touch Layer 28
5.3.1 UIKit Framework (UIKit.framework) 29
5.3.2 Map Kit Framework (MapKit.framework) 29
5.3.3 Push Notification Service 30
iii
5.3.4 Message ill Framework (MessageUl.framework) 30
5.3.5 Address Book ill Framework (Addressill.framework) 30
5.3.6 Game Kit Framework (GameKit.framework) 30
5.3.7 iAd Framework (iAd.framework) 30
5.3.8 Event Kit Ul Framework (EventKit.framework) 30
5.3.9 Accounts Framework (Accounts.framework) 31
5.3.10 Social Framework (Social.framework) 31
5.3.11 SpriteKit Framework (SpriteKit.framework) 31
5.4 The iOS Media Layer 31
5.4.1 Core Video Framework (CoreVideo.framework) 31
5.4.2 Core Text Framework (CoreText.framework) 31
5.4.3 Image I/O Framework (ImagelO.framework) 31
5.4.4 Assets Library Framework (AssetsLibrary.framework) 31
5.4.5 Core Graphics Framework (CoreGraphics.framework) 32
5.4.6 Core Image Framework (Corelmage.framework) 32
5.4.7 Quartz Core Framework (QuartzCore.framework) 32
5.4.8 OpenGL ESframework (OpenGLES.framework) 32
5.4.9 GLKit Framework (GLKitframework) 32
5.4.10 NewsstandKit Framework (NewsstandKit.framework) 32
5.4.11 iOS Audio Support. 32
5.4.12 AVFoundation framework (AVFoundation.framework) 33
5.4.13 Core Audio Frameworks [CoreAudio.framework, AudioToolbox.framework and
Audioilnit.framework) 33
5.4.14 Open Audio iibrary (OpenAL) 33
5.4.15 Media Player Framework (MediaPlayer.framework) 33
5.4.16 Core Midi Framework (CoreMIDI.framework) 33
5.5 The iOS Core Services Layer 33
5.5.1 Address Book Framework (AddressBook.framework) 33
5.5.2 CFNetwork Framework (CFNetwork.framework) 33
5.5.3 Core Data Framework (CoreData.framework) 33
5.5.4 Core Foundation Framework (CoreFoundation.framework) 34
5.5.5 Core Media Framework (CoreMedia.framework) 34
5.5.6 Core Telephony Framework (CoreTelephony.framework) 34
5.5.7 EventKit Framework (EventKit.framework) 34
5.6 Foundation Framework (Foundation.framework) 34
5.6.1 Core iocation Framework (CoreLocation.framework) 34
5.6.2 Mobile Core Services Framework (MobileCoreServices.framework) 34
5.6.3 Store Kit Framework (StoreKit.framework) 35
5.6.4 SQiite library 35
5.6.5 System Configuration Framework (SystemConfiguration.framework) 35
5.6.6 Quick Look Framework (QuickLook.framework) 35
iv
5.7 The iOS Core OS Layer 35
5.7.1 Accelerate Framework (Accelerate.framework) 35
5.7.2 External Accessory Framework (ExternalAccessory.framework) 35
5.7.3 Security Framework (Security.framework) 36
5.7.4 System (LibSystem) 36
6. Testing Apps on iOS 7 Devices with Xcode 5 37
6.1 Configuring Xcode with Apple IDs 37
6.2 Generating Signing Identities 38
6.3 Adding a Device to the Developer Portal 40
6.4 Running an Application on a Registered Device 40
6.5 Summary 41
7. The Basics of Objective-C Programming 43
7.1 Objective-C Data Types and Variables 43
7.2 Objective-C Expressions 44
7.3 Objective-C Flow Control with if and else 47
7.4 Looping with the for Statement 48
7.5 Objective-C Looping with do and while 49
7.6 Objective-C do ...while loops 49
8. The Basics of Object Oriented Programming in Objective-C 51
8.1 What is an Object? 51
8.2 What is a Class? 51
8.3 Declaring an Objective-C Class Interface 51
8.4 Adding Instance Variables to a Class 52
8.5 Define Class Methods 53
8.6 Declaring an Objective-C Class Implementation 54
8.7 Declaring and Initializing a Class Instance 55
8.8 Automatic Reference Counting (ARC) 55
8.9 Calling Methods and Accessing Instance Data 56
8.10 Objective-C and Dot Notation 56
8.11 How Variables are Stored 57
8.12 An Overview of Indirection 58
8.13 Indirection and Objects 60
8.14 Indirection and Object Copying 60
8.15 Creating the Program Section•
60
8.16 Bringing it all Together 61
8.17 Structuring Object-Oriented Objective-C Code 63
9. The Basics of Modern Objective-C 67
9.1 Default Property Synthesis 67
v
9.2 Method Ordering 68
9.3 NSNumber Literals 69
9.4 Array Literals 70
9.5 Dictionary Literals 71
9.6 Summary 71
10. An Overview of the iOS 7 Application Development Architecture 73
10.1 Model View Controller (MVC) 73
10.2 The Target-Action pattern, IBOutlets and IBActions 74
10.3 Subclassing 75
10.4 Delegation 75
10.5 Summary 75
11. Creating an Interactive iOS 7 App 77
11.1 Creating the New Project 77
11.2 Creating the User Interface 77
11.3 Building and Running the Sample Application 80
11.4 Adding Actions and Outlets 80
11.5 Building and Running the Finished Application 86
11.6 Summary 86
12. Writing iOS 7 Code to Hide the Keyboard 87
12.1 Creating the Example App 87
12.2 Hiding the Keyboard when the User Touches the Return Key 88
12.3 Hiding the Keyboard when the User Taps the Background 90
12.4 Summary 90
13. Understanding iOS 7 Views, Windows and the View Hierarchy 91
13.1 An Overview of Views 91
13.2 The UlWindow Class 91
13.3 The View Hierarchy 92
13.4 View Types 94
13.4.1 The Window 94
13.4.2 Container Views 94
13.4.3 Controls 95
13.4.4 Display Views 95
13.4.5 Text and Web Views 95
13.4.6 Navigation Views and Tab Bars 95
13.4.7 Alert Views and Action Sheets 95
13.5 Summary 95
14. An Introduction to Auto Layout in iOS 7 97
vi
14.1 An Overview of Auto Layout 97
14.2 Alignment Rects 99
14.3 Intrinsic Content Size 99
14.4 Content Hugging and Compression Resistance Priorities 99
14.5 Three Ways to Create Constraints 99
14.6 Constraints in more Detail 100
14.7 Summary 101
15. Working with iOS 7 Auto Layout Constraints in Interface Builder 103
15.1 A Simple Example of Auto Layout in Action 103
15.2 Enabling and Disabling Auto Layout in Interface Builder 103
15.3 The Auto Layout Features of Interface Builder 109
15.3.1 Suggested Constraints 109
15.3.2 Visual Cues 110
15.3.3 Highlighting Constraint Problems 112
15.3.4 Viewing, Editing and Deleting Constraints 114
15.4 Creating New Constraints in Interface Builder 116
15.5 Resolving Auto Layout Problems 117
15.6 Summary 119
16. An iOS 7 Auto Layout Example 121
16.1 Preparing the Project 121
16.2 Designing the User Interface 121
16.3 Adding Auto Layout Constraints 123
16.4 Adjusting Constraint Priorities 125
16.5 Testing the Application 128
16.6 Summary 128
17. Implementing iOS 7 Auto Layout Constraints in Code 129
17.1 Creating Constraints in Code 129
17.2 Adding a Constraint to a View 130
17.3 Turning off Auto Resizing Translation 131
17.4 An Example Application 132
17.5 Creating the Views 132
17.6 Creating and Adding the Constraints 132
17.7 Removing Constraints 135
17.8 Summary : 135
18. Implementing Cross-Hierarchy Auto Layout Constraints in iOS 7 137
18.1 The Example Application 137
18.2 Establishing Outlets 138
18.3 Writing the Code to Remove the Old Constraint 140
vii
18.4 Adding the Cross Hierarchy Constraint I40
18.5 Testing the Application 140
18.6 Summary 141
19. Understanding the iOS 7 Auto Layout Visual Format Language 143
19.1 Introducing the Visual Format Language I43
19.2 Visual Language Format Examples I43
19.3 Using the constraintsWithVisualFormat: Method I44
19.4 Summary 146
20. Using Storyboards in Xcode 5 147
20.1 Creating the Storyboard Example Project 147
20.2 Accessing the Storyboard I47
20.3 Adding Scenes to the Storyboard 149
20.4 Configuring Storyboard Segues 151
20.5 Configuring Storyboard Transitions 151
20.6 Associating a View Controller with a Scene 152
20.7 Passing Data Between Scenes 154
20.8 Unwinding Storyboard Segues 155
20.9 Triggering a Storyboard Segue Programmatically 156
20.10 Summary 157
21. Using Xcode 5 Storyboards to Create an iOS 7 Tab Bar Application 159
21.1 An Overview of the Tab Bar 159
21.2 Understanding View Controllers in a Multiview Application 159
21.3 Setting up the Tab Bar Example Application 160
21.4 Reviewing the Project Files 160
21.5 Renaming the Initial View Controller 160
21.6 Adding the View Controller for the Second Content View 161
21.7 Adding the Tab Bar Controller to the Storyboard 161
21.8 Adding a Second View Controller to the Storyboard 162
21.9 Designing the View Controller User interfaces 164
21.10 Configuring the Tab Bar Items 165
21.11 Building and Running the Application 165
21.12Summary 166
22. An Overview of iOS 7 Table Views and Xcode 5 Storyboards 167
22.1 An Overview of the Table View 167
22.2 Static vs. Dynamic Table Views 167
22.3 The Table View Delegate and dataSource 168
22.4 Table View Styles 168
22.5 Table View Cell Styles 169
viii
22.6 Table View Cell Reuse 169
22.7 Summary 171
23. Using Xcode 5 Storyboards to Build Dynamic TabieViews with Prototype Table View Cells 173
23.1 Creating the Example Project 173
23.2 Adding the TableView Controller to the Storyboard 174
23.3 Creating the UlTableViewController and UlTableViewCell Subclasses 175
23.4 Declaring the Cell Reuse Identifier 176
23.5 Designing a Storyboard UlTableView Prototype Cell 176
23.6 Modifying the CarTableViewCell Class 177
23.7 Creating the Table View Datasource 178
23.8 Downloading and Adding the Image Files 182
23.9 Compiling and Running the Application 182
23.10 Summary 183
24. Implementing iOS 7 TableView Navigation using Storyboards in Xcode 5 185
24.1 Understanding the Navigation Controller 185
24.2 Adding the New Scene to the Storyboard 186
24.3 Adding a Navigation Controller 187
24.4 Establishing the Storyboard Segue 187
24.5 Modifying the CarDetailViewController Class 188
24.6 Using prepareForSegue: to Pass Data between Storyboard Scenes 190
24.7 Testing the Application191
24.8 Summary 191
25. Using an Xcode 5 Storyboard to Create a Static Table View 193
25.1 An Overview of the Static Table Project 193
25.2 Creating the Project 193
25.3 Adding a Table View Controller 194
25.4 Changing the Table View Content Type 194
25.5 Designing the Static Table 195
25.6 Adding Items to the Table Cells 196
25.7 Modifying the StaticTableViewController Class 199
25.8 Building and Running the Application 199
25.9 Summary200
26. An iPad iOS 7 Split View and Popover Example 201
26.1 An Overview of Split View and Popovers 201
26.2 About the Example iPad Split View and Popover Project 202
26.3 Creating the Project 202
26.4 Reviewing the Project 202
26.5 Configuring Master View Items 203
ix
26.6 Configuring the Detail View Controller 205
26.7 Connecting Master Selections to the Detail View 206
26.8 Popover Implementation 206
26.9 Testing the Application 207
26.10 Summary 209
27. Implementing a Page based iOS 7 Application using UlPageViewController 211
27.1 The UlPageViewController Class 211
27.2 The UlPageViewController DataSource 211
27.3 Navigation Orientation 212
27.4 Spine Location 212
27.5 The UlPageViewController Delegate Protocol 213
27.6 Summary 213
28. An Example iOS 7 UlPageViewController Application 215
28.1 The Xcode Page-based Application Template 215
28.2 Creating the Project 215
28.3 Adding the Content View Controller 215
28.4 Creating the Data Model 218
28.5 Initializing the UlPageViewController 222
28.6 Running the UlPageViewController Application 224
28.7 Summary 224
29. Using the iOS 7 UlPickerView and UIDatePicker Components 225
29.1 The DatePicker and PickerView Components 225
29.2 A DatePicker Example 226
29.3 Designing the User Interface 226
29.4 Coding the Date Picker Example Functionality 227
29.5 Building and Running the Date Picker Application 228
30. An iOS 7 UlPickerView Example 229
30.1 Creating the iOS 7 PickerView Project 229
30.2 UlPickerView Delegate and DataSource 229
30.3 The PickerViewController.h File 230
30.4 Designing the User Interface 230
30.5 Initializing the Arrays 232
30.6 Implementing the DataSource Protocol 233
30.7 Implementing the Delegate Protocol 234
30.8 Hiding the Keyboard 234
30.9 Testing the Application 234
31. Working with Directories on iOS 7 237
x
31.1 The Application Documents Directory 237
31.2 The Objective-C NSFileManager, NSFileHandle and NSData Classes 237
31.3 Understanding Pathnames in Objective-C 238
31.4 Obtaining a Reference to the Default NSFileManager Object 238
31.5 Identifying the Current Working Directory 238
31.6 Identifying the Documents Directory 239
31.7 Identifying the Temporary Directory 240
31.8 Changing Directory 240
31.9 Creating a New Directory 241
31.10 Deleting a Directory 242
31.11 Listing the Contents of a Directory 242
31.12 Getting the Attributes of a File or Directory 243
32. Working with Files on iOS 7 245
32.1 Creating an NSFileManager Instance 245
32.2 Checking for the Existence of a File 245
32.3 Comparing the Contents of Two Files 246
32.4 Checking if a File is Readable/Writable/Executable/Deletable 246
32.5 Moving/Renaming a File 246
32.6 Copying a File 247
32.7 Removing a File 247
32.8 Creating a Symbolic Link 247
32.9 Reading and Writing Files with NSFileManager 248
32.10 Working with Files using the NSFileHandle Class 249
32.11 Creating an NSFileHandle Object 249
32.12 NSFileHandle File Offsets and Seeking 249
32.13 Reading Data from a File 250
32.14 Writing Data to a File 250
32.15 Truncating a File :251
32.16 Summary 252
33. iOS 7 Directory Handling and File I/O - A Worked Example 253
33.1 The Example Application 253
33.2 Setting up the Application Project 253
33.3 Designing the User Interface 253
33.4 Checking the Data File on Application Startup 254
33.5 Implementing the Action Method 256
33.6 Building and Running the Example 256
34. Preparing an iOS 7 App to use iCIoud Storage 259
34.1 What is iCIoud? 259
xi
I
34.2 iCIoud Data Storage Services 259
34.3 Preparing an Application to Use iCIoud Storage 260
34.4 Enabling iCIoud Support for an iOS 7 Application 260
34.5 Reviewing the iCIoud Entitlements File 260
34.6 Manually Creating the Entitlements File 261
34.7 Accessing Multiple Ubiquity Containers 262
34.8 Ubiquity Container URLs 262
34.9 Summary 263
35. Managing Files using the iOS 7 UIDocument Class 265
35.1 An Overview of the UIDocument Class 265
35.2 Subclassing the UIDocument Class 265
35.3 Conflict Resolution and Document States 266
35.4 The UIDocument Example Application 266
35.5 Creating a UIDocument Subclass 266
35.6 Designing the User Interface 267
35.7 Implementing the Application Data Structure 268
35.8 Implementing the contentsForType Method 268
35.9 Implementing the loadFromContents Method 269
35.10 Loading the Document at App Launch 269
35.11 Saving Content to the Document 272
35.12 Testing the Application 273
35.13 Summary 273
36. Using iCIoud Storage in an iOS 7 Application 275
36.1 iCIoud Usage Guidelines 275
36.2 Preparing the iCIoudStore Application for iCIoud Access 276
36.3 Configuring the View Controller 276
36.4 Implementing the viewDidLoad Method 277
36.5 Implementing the metadataQueryDidFinishGathering: Method 280
36.6 Implementing the saveDocument Method 283
36.7 Enabling iCIoud Document and Data Storage 283
36.8 Running the iCIoud Application 284
36.9 Reviewing and Deleting iCIoud Based Documents 284
36.10 Reviewing iCIoud Activities in the Xcode Debugging Debug Navigator 285
36.11 Making a Local File Ubiquitous 286
36.12 Summary 286
37. Synchronizing iOS 7 Key-Value Data using iCIoud 287
37.1 An Overview of iCIoud Key-Value Data Storage 287
37.2 Sharing Data Between Applications 288
xii
37.3 Data Storage Restrictions 288
37.4 Conflict Resolution 288
37.5 Receiving Notification of Key-Value Changes 289
37.6 An iCIoud Key-Value Data Storage Example 289
37.7 Enabling the Application for iCIoud Key Value Data Storage 289
37.8 Designing the User Interface 290
37.9 Implementing the View Controller 291
37.10 Modifying the viewDidLoad Method 291
37.11 Implementing the Notification Method 292
37.12 Implementing the saveData Method 292
37.13 Testing the Application 293
38. iOS 7 Data Persistence using Archiving 295
38.1 An Overview of Archiving 295
38.2 The Archiving Example Application 296
38.3 Designing the User Interface 296
38.4 Checking for the Existence of the Archive File on Startup 297
38.5 Archiving Object Data in the Action Method 299
38.6 Testing the Application 299
38.7 Summary 300
39. iOS 7 Database Implementation using SQLite 301
39.1 What is SQLite? 301
39.2 Structured Query Language (SQL) 302
39.3 Trying SQLite on MacOS X 302
39.4 Preparing an iOS Application Project for SQLite Integration 303
39.5 Key SQLite Functions 304
39.6 Declaring a SQLite Database 305
39.7 Opening or Creating a Database 305
39.8 Preparing and Executing a SQL Statement 305
39.9 Creating a Database Table 306
39.10 Extracting Data from a Database Table 307
39.11 Closing a SQLite Database 308
39.12 Summary 308
40. An Example SQLite based iOS 7 Application 309
40.1 About the Example SQLite Application 309
40.2 Creating and Preparing the SQLite Application Project 309
40.3 Importing sqlite3.h and declaring the Database Reference 309
40.4 Designing the User Interface 310
40.5 Creating the Database and Table 311
xiii
40.6 Implementing the Code to Save Data to the SQLite Database 313
40.7 Implementing Code to Extract Data from the SQLite Database 314
40.8 Building and Running the Application 315
40.9 Summary 316
41. Working with iOS 7 Databases using Core Data 317
41.1 The Core Data Stack 317
41.2 Managed Objects 318
41.3 Managed Object Context 318
41.4 Managed Object Model 319
41.5 Persistent Store Coordinator 319
41.6 Persistent Object Store 319
41.7 Defining an Entity Description 320
41.8 Obtaining the Managed Object Context 321
41.9 Getting an Entity Description 321
41.10 Creating a Managed Object 322
41.11 Getting and Setting the Attributes of a Managed Object 322
41.12 Fetching Managed Objects 322
41.13 Retrieving Managed Objects based on Criteria 323
41.14 Summary 323
42. An iOS 7 Core Data Tutorial 325
42.1 The Core Data Example Application 325
42.2 Creating a Core Data based Application 325
42.3 Creating the Entity Description 325
42.4 Adding a Storyboard to the Project 326
42.5 Adding a View Controller 328
42.6 Designing the User Interface 328
42.7 Saving Data to the Persistent Store using Core Data 330
42.8 Retrieving Data from the Persistent Store using Core Data 331
42.9 Building and Running the Example Application 332
42.10 Summary 332
43. An Overview of iOS 7 Multitouch, Taps and Gestures 333
43.1 The Responder Chain333
43.2 Forwarding an Event to the Next Responder 334
43.3 Gestures334
43.4 Taps 334
43.5 Touches334
43.6 Touch Notification Methods334
43.6.1 touchesBegan method 335
xiv
j
43.6.2 touchesMoved method 335
43.6.3 touchesEnded method 335
43.6.4 touchesCancelled method 335
43.7 Summary 335
44. An Example iOS 7 Touch, Multitouch and Tap Application 337
44.1 The Example iOS 7 Tap and Touch Application 337
44.2 Creating the Example iOS Touch Project 337
44.3 Designing the User Interface 337
44.4 Enabling Multitouch on the View 338
44.5 Implementing the touchesBegan Method 339
44.6 Implementing the touchesMoved Method 339
44.7 Implementing the touchesEnded Method 340
44.8 Getting the Coordinates of a Touch 340
44.9 Building and Running the Touch Example Application 341
45. Detecting iOS 7 Touch Screen Gesture Motions 343
45.1 The Example iOS 7 Gesture Application 343
45.2 Creating the Example Project 343
45.3 Designing the Application User Interface 343
45.4 Implementing the touchesBegan Method 345
45.5 Implementing the touchesMoved Method 345
45.6 Implementing the touchesEnded Method 345
45.7 Building and Running the Gesture Example 346
45.8 Summary 346
46. Identifying Gestures using iOS 7 Gesture Recognizers 347
46.1 The UIGestureRecognizer Class 347
46.2 Recognizer Action Messages 348
46.3 Discrete and Continuous Gestures 348
46.4 Obtaining Data from a Gesture 348
46.5 Recognizing Tap Gestures 348
46.6 Recognizing Pinch Gestures 349
46.7 Detecting Rotation Gestures 349
46.8 Recognizing Pan and Dragging Gestures 349
46.9 Recognizing Swipe Gestures 350
46.10 Recognizing Long Touch (Touch and Hold) Gestures 350
46.11 Summary 350
\7. An iOS 7 Gesture Recognition Tutorial 351
47.1 Creating the Gesture Recognition Project 351
47.2 Designing the User Interface 351
xv
47.3 Implementing the Action Methods 353
47.4 Testing the Gesture Recognition Application 354
48. An Overview of iOS 7 Collection View and Flow Layout 355
48.1 An Overview of Collection Views 355
48.2 The UlCollectionView Class 357
48.3 The UlCollectionViewCell Class 358
48.4 The UlCollectionReusableView Class 358
48.5 The UlCollectionViewFlowLayout Class 358
48.6 The UlCollectionViewLayoutAttributes Class 359
48.7 The UlCollectionViewDataSource Protocol 359
48.8 The UlCollectionViewDelegate Protocol 360
48.9 The UlCollectionViewDelegateFlowLayout Protocol 361
48.10 Cell and View Reuse 361
48.11 Summary 363
49. An iOS 7 Storyboard-based Collection View Tutorial 365
49.1 Creating the Collection View Example Project 365
49.2 Removing the Template View Controller 365
49.3 Adding a Collection View Controller to the Storyboard 365
49.4 Adding the Collection View Cell Class to the Project 367
49.5 Designing the Cell Prototype 368
49.6 Implementing the Data Model 369
49.7 Implementing the Data Source 370
49.8 Testing the Application 372
49.9 Setting Sizes for Cell Items 373
49.10 Changing Scroll Direction 375
49.11 Implementing a Supplementary View 376
49.12 Implementing the Supplementary View Protocol Methods 378
49.13 Deleting Collection View Items 379
49.14 Summary 380
50. Subclassing and Extending the iOS 7 Collection View Flow Layout 381
50.1 About the Example Layout Class 381
50.2 Subclassing the UlCollectionViewFlowLayout Class 381
50.3 Extending the New Layout Class 382
50.4 Implementing the layoutAttributesForltemAtlndexPath: Method 382
50.5 Implementing the layoutAttributesForElementslnRect: Method 384
50.6 Implementing the modifyLayoutAttributes: Method 384
50.7 Adding the New Layout and Pinch Gesture Recognizer 385
50.8 Implementing the Pinch Recognizer 386
xvi
50.9 Avoiding Image Clipping 388
50.10 Testing the Application 389
50.11 Summary 389
51. Drawing iOS 7 2D Graphics with Core Graphics 391
51.1 Introducing Core Graphics and Quartz 2D 391
51.2 The drawRect Method 391
51.3 Points, Coordinates and Pixels 391
51.4 The Graphics Context 392
51.5 Working with Colors in Quartz 2D 392
51.6 Summary 393
52. An iOS 7 Graphics Tutorial using Core Graphics and Core Image 395
52.1 The iOS Drawing Example Application 395
52.2 Creating the New Project 395
52.3 Creating the UlView Subclass 395
52.4 Locating the drawRect Method in the UlView Subclass 396
52.5 Drawing a Line 397
52.6 Drawing Paths 399
52.7 Drawing a Rectangle 400
52.8 Drawing an Ellipse or Circle 401
52.9 Filling a Path with a Color 402
52.10 Drawing an Arc 404
52.11 Drawing a Cubic Bezier Curve 405
52.12 Drawing a Quadratic Bezier Curve 406
52.13 Dashed Line Drawing 407
52.14 Drawing Shadows 408
52.15 Drawing Gradients 410
52.16 Drawing an Image into a Graphics Context 414
52.17 Image Filtering with the Core Image Framework 416
52.18 Summary 418
53. Basic iOS 7 Animation using Core Animation 419
53.1 UlView Core Animation Blocks 419
53.2 Understanding Animation Curves 420
53.3 Receiving Notification of Animation Completion 420
53.4 Performing Affine Transformations 421
53.5 Combining Transformations 421
53.6 Creating the Animation Example Application 422
53.7 Implementing the Interface File 422
53.8 Drawing in the UlView 422
xvii
53.9 Detecting Screen Touches and Performing the Animation 423
53.10 Building and Running the Animation Application 424
53.11 Summary 425
54. iOS 7 UIKit Dynamics - An Overview 427
54.1 Understanding UIKit Dynamics 427
54.2 The UIKit Dynamics Architecture 428
54.2.1 Dynamic Items 428
54.2.2 Dynamic Behaviors 428
54.2.3 The Reference View 428
54.2.4 The Dynamic Animator 429
54.3 Implementing UIKit Dynamics in an iOS 7 Application 429
54.4 Dynamic Animator Initialization 430
54.5 Configuring Gravity Behavior 430
54.6 Configuring Collision Behavior 431
54.7 Configuring Attachment Behavior 432
54.8 Configuring Snap Behavior 434
54.9 Configuring Push Behavior 434
54.10 The UIDynamicltemBehavior Class 436
54.11 Combining Behaviors to Create a Custom Behavior 437
54.12 Summary 437
55. An iOS 7 UIKit Dynamics Tutorial 439
55.1 Creating the UIKit Dynamics Example Project 439
55.2 Adding the Dynamic Items 439
55.3 Creating the Dynamic Animator Instance 441
55.4 Adding Gravity to the Views 442
55.5 Implementing Collision Behavior 443
55.6 Attaching a View to an Anchor Point 444
55.7 Implementing a Spring Attachment Between two Views 447
55.8 Summary 448
56. An Introduction to iOS 7 Sprite Kit Programming 449
56.1 What is Sprite Kit? 449
56.2 The Key Components of a Sprite Kit Game 450
56.2.1 Sprite Kit View 450
56.2.2 Scenes 450
56.2.3 Nodes 450
56.2.4 Physics Bodies 450
56.2.5 Physics World451
56.2.6 Actions 451
xviii
i
I
56.2.7 Transitions 451
56.2.8 Texture Atlas 452
56.3 An Example Sprite Kit Game Hierarchy 452
56.4 The Sprite Kit Game Rendering Loop 452
56.5 Summary 453
57. An iOS 7 Sprite Kit Game Tutorial 455
57.1 About the Sprite Kit Demo Game 455
57.2 Creating the SpriteKitDemo Project 456
57.3 Reviewing the SpriteKit Game Template Project 457
57.4 Creating the Game Scene Classes 459
57.5 Implementing the Welcome Scene 459
57.6 Transitioning to the Archery Scene 462
57.7 Preparing the Archery Scene 464
57.8 Adding the Texture Atlas 465
57.9 Obtaining a Texture from the Atlas 466
57.10 Preparing the Animation Texture Atlas 467
57.11 Animating the Archer Sprite Node 469
57.12 Creating the Arrow Sprite Node 469
57.13 Shooting the Arrow 470
57.14 Adding the Ball Sprite Node 471
57.15 Summary 474
58. An iOS 7 Sprite Kit Collision Handling Tutorial 475
58.1 Defining the Category Bit Masks 475
58.2 Assigning the Category Masks to the Sprite Nodes 476
58.3 Configuring the Collision and Contact Masks 477
58.4 Implementing the Contact Delegate 477
58.5 Implementing a Physics Joint Between Nodes 479
58.6 Game Over 481
58.7 Summary 483
59. An iOS 7 Sprite Kit Particle Emitter Tutorial 485
59.1 What is the Particle Emitter? 485
59.2 The Particle Emitter Editor 485
59.3 The SKEmitterNode Class 486
59.4 Using the Particle Emitter Editor 486
59.5 Particle Emitter Node Properties 488
59.5.1 Background 488
59.5.2 Particle Texture 488
59.5.3 Particle Birthrate 488
xix
1
59.5.4 Particle Life Cycle 488
59.5.5 Particle Position Range 488
59.5.6 Angle 489
59.5.7 Particle Speed 489
59.5.8 Particle Acceleration 489
59.5.9 Particle Scale 489
59.5.10 Particle Rotation 489
59.5.11 Particle Color 489
59.5.12 Particle Blend Mode 490
59.6 Experimenting with the Particle Emitter Editor 490
59.7 Bursting a Ball using Particle Emitter Effects 491
59.8 Adding the Burst Particle Emitter Effect 492
59.9 Summary 495
60. Integrating iAds into an iOS 7 App 497
60.1 iOS Advertising Options 497
60.2 Preparing to Run iAds within an Application 498
60.3 iAd Advertisement Formats 498
60.3.1 BannerAds 498
60.3.2 Interstitial Ads 499
60.3.3 Medium Rectangle Ads 500
60.3.4 Pre-Roll Video Ads 501
60.4 Creating an Example iAds Application 502
60.5 Adding the iAds Framework to the Xcode Project 502
60.6 Enabling Banner Ads 502
60.7 Adding a Medium Rectangle Ad 503
60.8 Implementing an Interstitial Ad 505
60.9 Configuring iAds Test Settings 507
60.10 Going Live with iAds 508
60.11 Summary 509
61. iOS 7 Multitasking, Background Transfer Service and Fetching 511
61.1 Understanding iOS Application States 511
61.2 A Brief Overview of the Multitasking Application Lifecycle 512
61.3 Checking for Multitasking Support 513
61.4 Enabling Multitasking for an iOS Application 513
61.5 Supported Forms of Background Execution 514
61.6 An Overview of Background Fetch 514
61.7 An Overview of Remote Notifications 517
61.8 An Overview of Local Notifications 517
61.9 An Overview of Background Transfer Service 517
xx
61.10 The Rules of Background Execution 518
61.11 Summary 518
62. An iOS 7 Background Transfer Service Tutorial 519
62.1 Creating the Example Project 519
62.2 The handleEventsForBackgroundURLSession Method 519
62.3 Designing the User Interface 520
62.4 Configuring the View Controller 521
62.5 Implementing the Session Delegate Methods 522
62.6 Testing the Application 525
62.7 Summary 526
63. Scheduling iOS 7 Local Notifications 527
63.1 Creating the Local Notification App Project 527
63.2 Adding a Sound File to the Project 527
63.3 Locating the Application Delegate Method 528
63.4 Scheduling the Local Notification 528
63.5 Testing the Application 529
63.6 Cancelling Scheduled Notifications 530
63.7 Immediate Triggering of a Local Notification 530
63.8 Summary 530
64. An Overview of iOS 7 Application State Preservation and Restoration 531
64.1 The Preservation and Restoration Process 531
64.2 Opting In to Preservation and Restoration 532
64.3 Assigning Restoration Identifiers 532
64.4 Default Preservation Features of UIKit 533
64.5 Saving and Restoring Additional State Information 534
64.6 Understanding the Restoration Process 535
64.7 Saving Genera! Application State 536
64.8 Summary 537
65. An iOS 7 State Preservation and Restoration Tutorial 539
65.1 Creating the Example Application 539
65.2 Trying the Application without State Preservation 539
65.3 Opting-in to State Preservation 539
65.4 Setting Restoration Identifiers 540
65.5 Encoding and Decoding View Controller State 541
65.6 Adding a Navigation Controller to the Storyboard 543
65.7 Adding the Third View Controller 544
65.8 Creating the Restoration Class 546
65.9 Summary 548
xxi
66. Integrating Maps into iOS 7 Applications using MKMapltem 549
66.1 MKMapltem and MKPIacemark Classes549
66.2 An Introduction to Forward and Reverse Geocoding 550
66.3 Creating MKPIacemark Instances552
66.4 Working with MKMapltem553
66.5 MKMapltem Options and Enabling Turn-by-Turn Directions 554
66.6 Adding Item Details to an MKMapltem555
66.7 Summary556
67. An Example iOS 7 MKMapltem Application557
67.1 Creating the Mapltem Project557
67.2 Designing the User Interface 557
67.3 Converting the Destination using Forward Geocoding 559
67.4 Launching the Map560
67.5 Building and Running the Application561
67.6 Summary562
68. Getting Location Information using the iOS 7 Core Location Framework 563
68.1 The Basics of Core Location 563
68.2 Configuring the Desired Location Accuracy 563
68.3 Configuring the Distance Filter 564
68.4 The Location Manager Delegate 564
68.5 Obtaining Location Information from CLLocation Objects 565
68.5.1 Longitude and Latitude 565
68.5.2 Accuracy 565
68.5.3 Altitude 565
68.6 Calculating Distances 565
68.7 Location Information and Multitasking 566
68.8 Summary 566
69. An Example iOS 7 Location Application 567
69.1 Creating the Example iOS 7 Location Project 567
69.2 Designing the User Interface 567
69.3 Creating the CLLocationManager Object 569
69.4 Implementing the Action Method 569
69.5 Implementing the Application Delegate Methods 569
69.6 Building and Running the Location Application 571
70. Working with Maps on iOS 7 with MapKit and the MKMapView Class 573
70.1 About the MapKit Framework 573
70.2 Understanding Map Regions 573
xxii
70.3 About the MKMapView Tutorial 574
70.4 Creating the Map Project 574
70.5 Adding the MapKit Framework to the Xcode Project 574
70.6 Adding the Navigation Controller 574
70.7 Creating the MKMapView Instance and Toolbar 575
70.8 Configuring the Map View 578
70.9 Changing the MapView Region 578
70.10 Changing the Map Type 578
70.11 Testing the MapView Application 579
70.12 Updating the Map View based on User Movement 580
70.13 Summary 580
71. Working with MapKit Local Search in iOS 7 583
71.1 An Overview of iOS 7 Local Search 583
71.2 Adding Local Search to the MapSample Application 585
71.3 Adding the Local Search Text Field 585
71.4 Performing the Local Search 586
71.5 Testing the Application 588
71.6 Summary 589
72. Using MKDirections to get iOS 7 Map Directions and Routes 591
72.1 An Overview of MKDirections 591
72.2 Adding Directions and Routes to the MapSample Application 593
72.3 Adding the New Classes to the Project 593
72.4 Configuring the Results Table View 594
72.5 Implementing the Result Table View Segue 596
72.6 Adding the Route Scene 598
72.7 Getting the Route and Directions 599
72.8 Establishing the Route Segue 601
72.9 Testing the Application 602
72.10 Summary 603
73. Using iOS 7 Event Kit to Create Date and Location Based Reminders 605
73.1 An Overview of the Event Kit Framework 605
73.2 The EKEventStore Class 605
73.3 Accessing Calendars in the Database 607
73.4 Accessing Current Reminders 608
73.5 Creating Reminders 608
73.6 Creating Alarms 609
73.7 Creating the Example Project 609
73.8 Designing the User Interface for the Date/Time Based Reminder Screen 609
xxiii
73.9 Implementing the Reminder Code 610
73.10 Hiding the Keyboard 612
73.11 Designing the Location-based Reminder Screen 613
73.12 Creating a Location-based Reminder 614
73.13 Testing the Application 617
73.14 Summary 618
74. Accessing the iOS 7 Camera and Photo Library 619
74.1 The UllmagePickerController Class 619
74.2 Creating and Configuring a UllmagePickerController Instance 619
74.3 Configuring the UllmagePickerController Delegate 620
74.4 Detecting Device Capabilities 622
74.5 Saving Movies and Images 622
74.6 Summary 623
75. An Example iOS 7 iPhone Camera Application 625
75.1 An Overview of the Application 625
75.2 Creating the Camera Project 625
75.3 Designing the User Interface 625
75.4 Implementing the Action Methods 628
75.5 Writing the Delegate Methods 629
75.6 Building and Running the Application 631
76. Video Playback from within an iOS 7 Application 633
76.1 An Overview of the MPMoviePlayerController Class 633
76.2 Supported Video Formats 633
76.3 The iOS Movie Player Example Application 633
76.4 Designing the User Interface 634
76.5 Declaring the MoviePlayer Instance 634
76.6 Implementing the Action Method 634
76.7 The Target-Action Notification Method 635
76.8 Build and Run the Application 635
77. Playing Audio on iOS 7 using AVAudioPlayer 637
77.1 Supported Audio Formats 637
77.2 Receiving Playback Notifications637
77.3 Controlling and Monitoring Playback 638
77.4 Creating the Audio Example Application 638
77.5 Adding an Audio File to the Project Resources 638
77.6 Designing the User Interface639
77.7 Implementing the Action Methods 640
77.8 Creating and Initializing the AVAudioPlayer Object 641
xxiv
77.9 Implementing the AVAudioPlayerDelegate Protocol Methods 641
77.10 Building and Running the Application 642
78. Recording Audio on iOS 7 with AVAudioRecorder 643
78.1 An Overview of the AVAudioRecorder Tutorial 643
78.2 Creating the Recorder Project 643
78.3 Designing the User Interface 643
78.4 Creating the AVAudioRecorder Instance 645
78.5 Implementing the Action Methods 646
78.6 Implementing the Delegate Methods 647
78.7 Testing the Application 648
79. Integrating Twitter and Facebook into iOS 7 Applications 649
79.1 The UlActivityViewController class 649
79.2 The Social Framework 649
79.3 Accounts Framework 650
79.4 Using the UlActivityViewController Class 651
79.5 Using the SLComposeViewController Class 653
79.6 Summary 654
80. An iOS 7 Facebook Integration Tutorial using UlActivityViewController 655
80.1 Creating the Facebook Social App 655
80.2 Designing the User Interface 655
80.3 Creating Outlets and Actions 657
80.4 Implementing the selectlmage and Delegate Methods 658
80.5 Hiding the Keyboard 659
80.6 Posting the Message to Facebook 659
80.7 Running the Social Application 660
80.8 Summary 661
81. iOS 7 Facebook and Twitter Integration using SLRequest 663
81.1 Using SLRequest and the Account Framework 663
81.2 Twitter Integration using SLRequest 664
81.3 Facebook Integration using SLRequest 667
81.4 Summary 669
82. An iOS 7 Twitter Integration Tutorial using SLRequest 671
82.1 Creating the TwitterApp Project 671
82.2 Designing the User Interface 671
82.3 Modifying the Interface File 673
82.4 Accessing the Twitter API 673
82.5 Calling the getTimeLine Method 676
XXV
82.6 The Table View Delegate Methods 676
82.7 Building and Running the Application 677
82.8 Summary 678
83. Making Store Purchases with the SKStoreProductViewController Class 679
83.1 The SKStoreProductViewController Class 679
83.2 Creating the Example Project 680
83.3 Creating the User Interface 680
83.4 Displaying the Store Kit Product View Controller 681
83.5 Implementing the Delegate Method 683
83.6 Adding the Store Kit Framework to the Build Phases 683
83.7 Testing the Application 683
83.8 Summary 685
84. Building In-App Purchasing into iOS 7 Applications 687
84.1 In-App Purchase Options 687
84.2 Uploading App Store Hosted Content 688
84.3 Configuring In-App Purchase Items 688
84.4 Sending a Product Request 688
84.5 Accessing the Payment Queue 689
84.6 The Transaction Observer Object 690
84.7 Initiating the Purchase 690
84.8 The Transaction Process 690
84.9 Transaction Restoration Process 692
84.10 Testing In-App Purchases 693
84.11 Summary 693
85. Preparing an iOS 7 Application for In-App Purchases 695
85.1 About the Example Application 695
85.2 Creating the Xcode Project 695
85.3 Registering and enabling the App ID for In App Purchasing 695
85.4 Configuring the Application in iTunes Connect 696
85.5 Creating an In-App Purchase Item 697
85.6 Summary 698
86. An iOS 7 In-App Purchase Tutorial 699
86.1 The Application User Interface 699
86.2 Designing the Storyboard 700
86.3 Creating the Purchase View Controller 701
86.4 Completing the InAppDemoViewController Class 702
86.5 Completing the PurchaseViewController Class 704
86.6 Testing the Application 707
xxvi
86.7 Troubleshooting 708
86.8 Summary 708
87. Configuring and Creating App Store Hosted Content for iOS 7 In-App Purchases 709
87.1 Configuring an Application for In-App Purchase Hosted Content 709
87.2 The Anatomy of an In-App Purchase Hosted Content Package 710
87.3 Creating an In-App Purchase Hosted Content Package 710
87.4 Archiving the Hosted Content Package 711
87.5 Validating the Hosted Content Package 712
87.6 Uploading the Hosted Content Package 713
87.7 Summary 713
88. Preparing and Submitting an iOS 7 Application to the App Store 715
88.1 Verifying the iOS Distribution Certificate 715
88.2 Adding Icons and Launch Images to the Application 717
88.3 Targeting 32-bit and 64-bit Architectures 718
88.4 Archiving the Application for Distribution 719
88.5 Configuring the Application in iTunes Connect 720
88.6 Validating and Submitting the Application 721
89. Promoting your iOS Apps using iAd Workbench 723
89.1 An Overview of iAd Workbench 723
89.2 Creating a New iAds Campaign 724
89.3 Campaign Targeting Options 725
89.4 Designing the Banner 727
89.5 Summary 728
Index 729
xxvii