Learning S wift BUILDING APPS FOR OS X AND IOS Jonathon Manning, Paris Buttfield-Addison & Tim Nugent Learning Swift Jon Manning, Paris Buttfield-Addison, and Tim Nugent BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Learning Swift by Jon Manning, Paris Buttfield-Addison, and Tim Nugent Copyright © 2016 Secret Lab. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editors: Brian MacDonald and Rachel Roumeliotis Indexer: Ellen Troutman-Zaig Production Editor: Melanie Yarbrough Interior Designer: David Futato Copyeditor: Rachel Monaghan Cover Designer: Karen Montgomery Proofreader: Amanda Kersey Illustrator: Rebecca Demarest May 2016: First Edition Revision History for the First Edition 2016-04-29: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491940747 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning Swift, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-94074-7 [LSI] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Part I. Swift Basics 1. Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The Apple Developer Program 5 Registering for the Apple Developer Program 6 Downloading Xcode 7 Creating Your First Project with Xcode 8 The Xcode Interface 12 Developing a Simple Swift Application 20 Designing the Interface 21 Connecting the Code 22 Using the iOS Simulator 24 Conclusion 26 2. The Basics of Swift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 The Swift Programming Language 28 Swift 1 Versus Swift 2 30 Playgrounds 30 Comments 32 Control Flow 33 Loops 33 Switches 34 Variables and Constants 37 Operators 38 Types 39 iii Working with Strings 39 Comparing Strings 40 Searching Strings 41 Optional Types 41 Type Casting 43 Tuples 44 Arrays 45 Dictionaries 47 Enumerations 47 Sets 49 Functions and Closures 50 Using Functions as Variables 53 Closures 55 The defer Keyword 57 The guard Keyword 57 Conclusion 58 3. Swift for Object-Oriented App Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Classes and Objects 59 Initialization and Deinitialization 61 Properties 62 Inheritance 62 Protocols 66 Extensions 67 Access Control 68 Operator Overloading 70 Generics 71 Subscripts 72 Structures 73 Modules 74 The Swift Standard Library, Foundation, Cocoa, and Cocoa Touch 74 Data 75 Loading Data from Files and URLs 75 Serialization and Deserialization 76 Error Handling 77 Memory Management 80 Design Patterns in Cocoa and Cocoa Touch 81 Model-View-Controller 81 Delegation 83 Structuring an App 85 The Application Delegate 85 Window Controllers and View Controllers 85 iv | Table of Contents Nibs and Storyboards 86 Conclusion 86 Part II. An OS X App 4. Setting Up the OS X Notes App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Designing the OS X Notes App 90 Creating the OS X Project 93 Defining a Document Type 98 Adding the Icon 102 Conclusion 104 5. Working with Documents on OS X. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 The NSDocument Class 105 Storing Data in the Document 106 Storing Text 107 Package File Formats 108 The guard Keyword, and Why It’s Great 112 Saving Files 113 Loading Files 116 A Basic UI 117 Conclusion 126 6. User Interfaces and iCloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Updating the UI 127 Document-Filetype-Extension UI 131 Getting an Icon for the Collection View Cells 136 Adding Attachments 137 Storing and Managing Attachments 145 Displaying Data in the Collection View 149 Enhancing Attachments 152 Opening Attachments 152 JSON Attachments 155 Adding Attachments via Drag-and-Drop 158 Adding QuickLook 161 iCloud 166 The Basics of iCloud 167 Conclusion 170 Table of Contents | v Part III. An iOS App 7. Setting Up the iOS Notes App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Designing the iOS Notes App 174 Creating the iOS Project 181 Enabling the iOS App for iCloud 185 Defining a Document Type 189 Conclusion 191 8. Working with Files in iCloud. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 The App Sandbox 193 iCloud Availability 195 Creating the Document List View Controller 196 View Controllers and Storyboards 198 The Navigation Controller 198 Collection Views 202 Using Constraints to Control Size and Position 204 Creating the Document Class 208 Listing Documents 214 Creating Documents 224 Downloading from iCloud 227 Deleting Documents 231 Renaming Documents 238 Conclusion 242 9. Working with Documents on iOS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Adding a View to Display Notes 243 Editing and Saving Documents 252 Conclusion 253 10. Working with Files and File Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Setting Up the Interface for Attachments 255 Listing Attachments 259 Determining Types of Attachments 261 Displaying Attachment Cells 264 Dealing with Conflicts 270 Creating the Quick Look Thumbnail 276 Conclusion 279 11. Images and Deletion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Adding Attachments 281 Adding Image Attachments 283 vi | Table of Contents Viewing Attachments 289 Deleting Attachments 300 Conclusion 307 12. Supporting the iOS Ecosystem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Sharing with UIActivityController 309 Handoffs 312 Searchability 317 Conclusion 319 13. Extending iOS Apps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 Searching with a Spotlight Indexing Extension 322 Today Widgets 332 Conclusion 340 14. Multimedia, Contacts, Location, and Notifications. . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Location Attachments 343 Audio Attachments 358 Video Attachments 371 Contact Attachments 379 Notifications 383 Conclusion 395 15. Polishing the iOS App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Speaking Text 397 Opening Links in SFSafariViewController 399 3D Touch 403 Home Screen Quick Actions 404 Peek and Pop 404 Settings 405 Undo Support 407 Images with Filters 410 Worldwide Apps 414 Internationalization 415 Localization 418 Accessibility 423 Splitscreen Multitasking 428 Conclusion 429 Table of Contents | vii Part IV. Extending Your Apps 16. Building a watchOS App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Designing for the Watch 434 Designing Our watchOS App 436 Creating the watchOS Extension 438 Communicating with the iPhone 442 User Interfaces for the Apple Watch 454 Showing Note Contents 460 Creating New Notes 466 Adding Handoff Between the Watch and the iPhone 468 Glances 471 Conclusion 475 17. Code Quality and Distribution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Debugging 477 Instruments 480 Testing 485 Unit Testing 486 UI Testing 487 Using Objective-C and Swift in the Same Project 489 Using Swift Objects in Objective-C 489 Using Objective-C Objects in Swift 490 The App Store 491 App Thinning 492 Testing iOS Apps with TestFlight 493 Conclusion 494 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 viii | Table of Contents
Description: