You are on page 1of 9

CONTENTS

Chapter 1: Building Adaptive User Interfaces


Adaptive UI demo ...................................................................................................................................... 9
Creating the Xcode Project ......................................................................................................................... 9
Adding Auto Layout Constraints ................................................................................................................ 14
Content Hugging Priority ........................................................................................................................... 21
Editing the Relation of the Constraints ...................................................................................................... 22
Size Classes ................................................................................................................................................ 24
Using Size Classes for Font Customization ............................................................................................... 26
Using Size Classes to Customize a View ................................................................................................... 27
Using Size Classes to Customize Constraints ............................................................................................ 37
Summary ..................................................................................................................................................... 39

Chapter 2: Adding Sections and Index list in UITableView


A Brief Look at the Demo App ................................................................................................................... 42
Download the Xcode Project Template ...................................................................................................... 43
Displaying Sections in UITableView ................................................................................................... 44
Adding An Index List to UITableView ................................................................................................ 49
Adding An A-Z Index List .......................................................................................................................... 50
Customizing Section Headers ..................................................................................................................... 52
Summary ..................................................................................................................................................... 52

Chapter 3: Animating Table View Cells


Creating a Simple Fade-in Animation for Table View Cells ...................................................................... 55
Creating a Rotation Effect Using CATransform3D .............................................................................. 56
Creating a Fly-in Effect using CATransform3DTranslate ............................................................. 58
Your Exercise .............................................................................................................................................. 59
Summary ..................................................................................................................................................... 59

Chapter 4: Working with JSON


Demo App ................................................................................................................................................... 61
Creating JSON Data Model ...................................................................................................................... 64

i
Fetching Loans with the Kiva API ............................................................................................................. 65
Displaying Loans in A Table View ............................................................................................................. 71
Compile and Run the App .......................................................................................................................... 72

Chapter 5: How to Integrate Facebook and Twitter Sharing


Create the Demo Project and Design the Interface ................................................................................... 75
Adding Twitter Support .............................................................................................................................. 77
Adding Facebook Support .......................................................................................................................... 79
Summary ..................................................................................................................................................... 81

Chapter 6: Working with Email and Attachments


Starting with the Xcode Project Template .................................................................................................. 84
Creating Email Using the MessageUI Framework ................................................................................ 85

Chapter 7: Sending SMS and MMS Using MessageUI Framework


A Glance at the Demo App ......................................................................................................................... 92
Getting Started ............................................................................................................................................ 92
Implementing the Delegate ......................................................................................................................... 93
Bring Up the Message Composer ............................................................................................................... 94
Sending MMS ............................................................................................................................................. 95
What if You Don't Want In-App SMS ........................................................................................................ 97
Wrap Up ..................................................................................................................................................... 97

Chapter 8: How to Get Direction and Draw Route on Maps


Sample Route App ...................................................................................................................................... 100
Creating an Action Method for the Direction Button ................................................................................. 101
Displaying the User Location on Maps ...................................................................................................... 103
Testing Location Using the Simulator ........................................................................................................ 105
Using MKDirections API to Get the Route info ................................................................................. 106
Scale the Map to Make the Route Fit Perfectly .......................................................................................... 110
Using Segmented control ........................................................................................................................... 111
Showing Route Steps .................................................................................................................................. 115

Chapter 9: Search Nearby Points of Interest Using Local Search


Local Search Demo App ............................................................................................................................. 124
Adding a Nearby Button in Storyboard ...................................................................................................... 124

ii
Search Nearby Restaurants and Adding annotations .................................................................................. 125
Testing the Demo App ................................................................................................................................ 127
Modifying the Pin Color for the Nearby Restaurants ................................................................................. 128

Chapter 10: Audio Recording and Playback


A Simple Demo App ................................................................................................................................... 132
Audio Recording using AVAudioRecorder ........................................................................................ 133
Implementing the Record Button ............................................................................................................... 137
Using Microphone Without Users' Permission ........................................................................................... 139
Implementing the Stop Button .................................................................................................................... 140
Implementing the AVAudioRecorderDelegate Protocol ................................................................ 141
Playing Audio Using AVAudioPlayer ................................................................................................. 141
Implementing the AVAudioPlayerDelegate Protocol .................................................................... 142
Compile and Run Your App ........................................................................................................................ 143
Implementing the Timer ............................................................................................................................. 144

Chapter 11: Scan QR Code Using AVFoundation Framework


Creating a QR Code Reader App ................................................................................................................ 149
Import AVFoundation Framework ....................................................................................................... 151
Implementing Video Capture ...................................................................................................................... 152
Implementing QR Code Reading ............................................................................................................... 155
Initializing the Green Box .......................................................................................................................... 155
Decoding the QR Code ............................................................................................................................... 156
Your Exercise - Barcode Reader ................................................................................................................. 158

Chapter 12: Working with URL Schemes


Sample QR Codes ....................................................................................................................................... 161
Using URL Schemes .................................................................................................................................. 162
Creating Your Custom URL Scheme .......................................................................................................... 165

Chapter 13: Building a Full Screen Camera


Demo App ................................................................................................................................................... 173
Configuring a Session ................................................................................................................................. 175
Selecting the Input Device .......................................................................................................................... 176
Configuring an Output Device ................................................................................................................... 177
Coordinating the Input and Output using Session ...................................................................................... 177

iii
Creating a Preview Layer and Start the Session ......................................................................................... 177
Capture a Still Image .................................................................................................................................. 178
Toggle between Front and Back Facing Camera Using Gestures .............................................................. 179
Zoom in and Out ......................................................................................................................................... 181
Saving Images to the Photo Album ............................................................................................................ 183

Chapter 14: Video Capturing and Playback Using AVKit


Configuring a Session ................................................................................................................................. 187
Selecting the Input Device .......................................................................................................................... 188
Configuring an Output Device ................................................................................................................... 189
Coordinating the Input and Output using the Capture Session .................................................................. 189
Creating a Preview Layer and Starting the Session .................................................................................... 190
Saving Video Data to a Movie File ............................................................................................................ 190
Using AVKit for Video Playback ............................................................................................................ 193
Implement the AVCaptureFileOutputRecordingDelegate Protocol ...................................... 195

Chapter 15: Displaying Banner Ads using Google AdMob


Apply a Google AdMob Account ............................................................................................................... 199
Using Google Mobile Ads Framework ....................................................................................................... 202
Displaying Banner Ads in Table View Header ........................................................................................... 204
Adding a Subtle Animation ........................................................................................................................ 207
Displaying a Sticky Banner Ad .................................................................................................................. 208
Working with Interstitial Ads ..................................................................................................................... 210

Chapter 16: Working with Custom Fonts


Download Custom Fonts ............................................................................................................................ 215
Adding Font Files to the Project ................................................................................................................. 215
Register the Fonts in the Project Info Settings ........................................................................................... 218
Using Custom Fonts in Interface Builder ................................................................................................... 218
Using Custom Fonts in Code ...................................................................................................................... 219

Chapter 17: Working with AirDrop and UIActivityViewController


UIActivityViewController Overview ........................................................................................ 223
Demo App ................................................................................................................................................... 224
Adding a Share Button in Interface Builder ............................................................................................... 225
Implementing AirDrop for File Sharing ..................................................................................................... 228

iv
Build and Run the AirDrop Demo .............................................................................................................. 229
Uniform Type Identifiers ............................................................................................................................ 230
Summary ..................................................................................................................................................... 231

Chapter 18: Building Grid Layouts with Collection Views


Getting Started with UICollectionView and UICollectionViewController ................... 233
Creating a New Project ............................................................................................................................... 234
Creating Custom Classes for the Collection View ..................................................................................... 238
Implementing the Collection View Controller ........................................................................................... 240
Customizing the Collection Cell Background ............................................................................................ 244

Chapter 19: Interacting with Collection Views


Designing the Detail View Controller ........................................................................................................ 247
Connecting the Controllers Using Segues .................................................................................................. 252
Creating a Custom Class for the Detail View Controller ........................................................................... 254
Data Passing Between Controllers ............................................................................................................. 255
Handling Multiple Selections ..................................................................................................................... 257
Ready to Test Your App .............................................................................................................................. 264

Chapter 20: Adaptive Collection Views Using Size Classes and UITraitCollection
Creating the Demo Project ......................................................................................................................... 267
Diving into the Code .................................................................................................................................. 269
Designing for size classes ........................................................................................................................... 272
Understanding Trait Collections ................................................................................................................. 273
Adaptive Collection View .......................................................................................................................... 274
Respond to the Change of Size Class ......................................................................................................... 276
Your Exercise .............................................................................................................................................. 277

Chapter 21: Building a Today Widget Using App Extensions


Understanding How App Extensions Work ................................................................................................ 280
The Today Extension .................................................................................................................................. 281
Code Sharing with Embedded Framework ................................................................................................. 283
Creating an Embedded Framework ............................................................................................................ 283
Moving Common Files to the Framework ................................................................................................. 286
Creating the Today Widget ......................................................................................................................... 289
Sharing Data with the Container App ......................................................................................................... 298

v
Chapter 22: Building Slide Out Sidebar Menus
A Glance at the Demo App ......................................................................................................................... 304
Creating the Xcode Project ......................................................................................................................... 305
Using the SWRevealViewController Library ................................................................................ 306
Associate the Front View and Rear View Controller .................................................................................. 308
Handling Menu Item Selection ................................................................................................................... 312
Customizing the Menu ............................................................................................................................... 313
Adding a Right Sidebar .............................................................................................................................. 314
Refactoring the Code with Swift Extensions .............................................................................................. 317

Chapter 23: View Controller Transitions and Animations


Demo Project .............................................................................................................................................. 322
Applying the Standard Transition ............................................................................................................... 323
Creating a Slide Down Animator ............................................................................................................... 324
Reversing the Transition ............................................................................................................................. 330
Creating the Slide Right Transition Animator ............................................................................................ 334
Creating a Pop Transition Animator ........................................................................................................... 338
Creating a Rotation Transition Animator ................................................................................................... 341

Chapter 24: Building a Slide Down Menu


Presenting the Menu Modally .................................................................................................................... 349
Creating the Animated Slide Down Menu .................................................................................................. 353
Building the Slide Down Menu Animator .................................................................................................. 354
Detecting Tap Gesture ................................................................................................................................ 357

Chapter 25: Self Sizing Cells and Dynamic Type


Building a Simple Demo ............................................................................................................................ 362
Adding Auto Layout Constraints ................................................................................................................ 363
Setting Row Height .................................................................................................................................... 365
Dynamic Type Introduction ........................................................................................................................ 366
Adopting Dynamic Type ............................................................................................................................ 368
Responding to Text Size Change ................................................................................................................ 370
Using Custom Font ..................................................................................................................................... 372

vi
Chapter 26: XML Parsing and RSS
Demo App ................................................................................................................................................... 377
A Sample RSS Feed .................................................................................................................................... 378
Building the Feed Parser ............................................................................................................................ 382
Expanding and Collapsing Table View Cells ............................................................................................. 388

Chapter 27: Applying a Blurred Background Using UIVisualEffect


The Demo App ........................................................................................................................................... 396
Understanding UIVisualEffect and UIVisualEffectView .................................................... 396
Adding a Background Image View ............................................................................................................ 398
Applying a Blurring Effect ......................................................................................................................... 400

Chapter 28: Using Touch ID For Authentication


Touch ID Demo App .................................................................................................................................. 405
Designing the User Interface ...................................................................................................................... 406
Using Local Authentication Framework .................................................................................................... 406
Password Authentication ............................................................................................................................ 411

Chapter 29: Building a Carousel-Like User Interface


Designing the Storyboard ........................................................................................................................... 415
Creating a Custom Class for the Collection View Cell .............................................................................. 426
Creating the Model Class ........................................................................................................................... 427
Populating the Collection View .................................................................................................................. 428
Handling the Like Button ........................................................................................................................... 432

Chapter 30: Working with Parse


Creating Your App on Parse ....................................................................................................................... 437
Setting up Your Data ................................................................................................................................... 439
Configuring the Xcode Project for Parse .................................................................................................... 443
Connecting with Parse ................................................................................................................................ 445
Retrieving Data from Parse ........................................................................................................................ 447
Refreshing Data .......................................................................................................................................... 451
Caching for Speed and Offline Access ....................................................................................................... 453
Updating Data on Parse .............................................................................................................................. 454
Deleting Data from Parse ........................................................................................................................... 456

vii
Chapter 31: How to Preload a SQLite Database Using Core Data
A Simple Demo App ................................................................................................................................... 460
The CSV File .............................................................................................................................................. 462
Parsing CSV Files ....................................................................................................................................... 463
Preloading the Data and Saving it into Database ....................................................................................... 466
Using External Data Files ........................................................................................................................... 468
Using An Existing Database in Your Project .............................................................................................. 471

Chapter 32: Connecting Multiple Annotations with Polylines and Routes


The Sample Route App ............................................................................................................................... 478
Detecting a Touch Using Gesture Recognizers .......................................................................................... 479
Applying Gesture Recognizers ................................................................................................................... 479
Pinning a Location on the Map .................................................................................................................. 480
Drop Pin Animation .................................................................................................................................... 482
Connecting Annotations with Polylines ..................................................................................................... 484
Connecting Annotations with Routes ......................................................................................................... 487
Removing Annotations ............................................................................................................................... 491

Chapter 33: Using CocoaPods in Swift Projects


What is CocoaPods? ............................................................................................................................... 494
Setting Up CocoaPods on Your Mac ..................................................................................................... 496
Using CocoaPods for Xcode Projects .................................................................................................... 496
Using the Xcode Workspace for Development .......................................................................................... 499
Wrapping Up .............................................................................................................................................. 500

Chapter 34: Building a Simple Sticker App


Preparing the Sticker Images ...................................................................................................................... 502
Building the Sticker Package Project Using Xcode ................................................................................... 503
Adding Images to the Sticker Pack ............................................................................................................ 505
Adding App Icons ....................................................................................................................................... 506
Testing the Sticker Pack ............................................................................................................................. 508
Enhancing Your Sticker Pack with Animated Images ................................................................................ 509
Adding a Sticker Pack for an Existing App ................................................................................................ 511
Summary ..................................................................................................................................................... 513

viii
Chapter 35: Building iMessage Apps Using Messages Framework
The Demo App ........................................................................................................................................... 517
Creating the Message Extension ................................................................................................................ 518
Sharing Code Using an Embedded Framework ......................................................................................... 521
Create the IconDataKit framework .................................................................................................... 521
Defining the iconSet array .................................................................................................................... 523
Moving the Icon Class from CollectionViewDemo to the Framework ............................................ 524
Replacing the Value of the iconSet Variable ........................................................................................ 525
Designing the UI of the iMessage App ................................................................................................. 526
Implementing MessagesViewController ...................................................................................... 529
Adding a Message to the Conversion ......................................................................................................... 534
Displaying the Icon Details ........................................................................................................................ 540
Designing the Detail View Controller ........................................................................................................ 540
Creating a New Class for the Detail View Controller ................................................................................ 543
Managing Message Selections and Extension States ................................................................................. 545
Summary ..................................................................................................................................................... 549

Chapter 36: Building Custom UI Components Using IBDesignable and IBInspectable


Understanding IBInspectable and IBDesignable ..................................................................... 551
Creating a Fancy Button ............................................................................................................................. 554
Corner Radius, Border Width and Border Color ........................................................................................ 556
Title and Image Padding ............................................................................................................................. 558
Buttons with Images ................................................................................................................................... 559
Image Padding ............................................................................................................................................ 560
Aligning the Image to the Right of the Title ............................................................................................... 562
Color Gradient ............................................................................................................................................ 564
Summary ..................................................................................................................................................... 567

ix

You might also like