OS X App Development with CloudKit and Swift — Bruce Wade OS X App Development with CloudKit and Swift Bruce Wade OS X App Development with CloudKit and Swift Bruce Wade Suite No. 1408, North Vancouver, British Columbia, Canada ISBN-13 (pbk): 978-1-4842-1879-2 ISBN-13 (electronic): 978-1-4842-1880-8 DOI 10.1007/978-1-4842-1880-8 Library of Congress Control Number: 2016941345 Copyright © 2016 by Bruce Wade This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Louise Corrigan Development Editor: James Markham Technical Reviewer: Charlie Cruz Editorial Board: Steve Anglin, Pramila Balen, Louise Corrigan, James DeWolf, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing Coordinating Editor: Nancy Chen Copy Editor: April Rondeau Compositor: SPi Global Indexer: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected] , or visit w ww.springer.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected] , or visit w ww.apress.com . Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at w ww.apress.com/bulk-sales . Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Printed on acid-free paper Contents at a Glance About the Author .....................................................................................................ix About the Technical Reviewer .................................................................................xi Introduction ...........................................................................................................xiii ■ Chapter 1: Introduction .........................................................................................1 ■ Chapter 2: Prototyping Our App.............................................................................5 ■ Chapter 3: Defi ning Our Data ...............................................................................39 ■ Chapter 4: Introduction to CloudKit .....................................................................63 ■ Chapter 5: Creating Test Data with CloudKit Dashboard .....................................75 ■ Chapter 6: Refi ning Our Prototype .......................................................................83 ■ Chapter 7: Updating CloudKit Data from Our App ..............................................101 ■ Chapter 8: Adding Local Cache to Improve Performance ..................................119 Index .....................................................................................................................129 iii Contents About the Author .....................................................................................................ix About the Technical Reviewer .................................................................................xi Introduction ...........................................................................................................xiii ■ Chapter 1: Introduction .........................................................................................1 Goals of This book ............................................................................................................1 Assumptions about the Reader ........................................................................................1 Software Requirements ....................................................................................................2 Downloading Sketch 3 ............................................................................................................................2 Downloading and Installing Keynote ......................................................................................................2 Downloading and Installing Xcode .........................................................................................................3 About the App We Are Going to Be Creating .....................................................................4 Conclusion ........................................................................................................................4 ■ Chapter 2: Prototyping Our App.............................................................................5 Introduction to Sketch 3 ...................................................................................................5 Our Prototype Objective....................................................................................................9 Building the Prototype with Sketch 3 .............................................................................10 Creating the Structure of the Main Window..........................................................................................11 Creating the New Park Button ..............................................................................................................16 Creating the Search Field .....................................................................................................................17 Creating the Park List ...........................................................................................................................18 Creating a New Group ...........................................................................................................................22 Creating Another Main Content Area ....................................................................................................24 v ■ CONTENTS Making the Prototype More Real ....................................................................................26 Reviewing the Features We Want to Display Using KeyNote ..........................................29 Searching for a Park .............................................................................................................................29 Adding a New Park ...............................................................................................................................32 Exporting Artboards to Use Inside KeyNote ..........................................................................................35 Using KeyNote to Make a Realistic Demo .............................................................................................35 Conclusion ......................................................................................................................37 ■ Chapter 3: Defi ning Our Data ...............................................................................39 Taking a Closer Look at Our Mockup ..............................................................................39 Dog Park Data Types ......................................................................................................40 Creating Our Project in Xcode ........................................................................................40 Update the Main.storyboard ...........................................................................................41 Creating the Left Sidebar ...............................................................................................41 Fixing the App’s Colors to Match Our Mockup ................................................................46 Adding the Search Box ...................................................................................................52 Implementing the Parks List ..........................................................................................53 Setting Up the Detail View ..............................................................................................55 Fixing the Collection View Item ......................................................................................60 Conclusion ......................................................................................................................62 ■ Chapter 4: Introduction to CloudKit .....................................................................63 iCloud Accounts ..............................................................................................................63 Containers ......................................................................................................................63 Databases ......................................................................................................................64 Records ..........................................................................................................................64 Record Zones ........................................................................................................................................64 Record Identifi ers .................................................................................................................................64 References .....................................................................................................................65 Assets .............................................................................................................................65 Convenience API .............................................................................................................65 vi ■ CONTENTS Queries ...........................................................................................................................66 Subscriptions .................................................................................................................67 CloudKit User Accounts ..................................................................................................68 CloudKit Dashboard ........................................................................................................69 Schema Record Types ....................................................................................................70 Security Roles ................................................................................................................72 Subscription Types .........................................................................................................72 Public Data User Records ...............................................................................................72 Default Zone ...................................................................................................................73 Usage .............................................................................................................................73 Private Data Default Zone ...............................................................................................73 Admin Team ....................................................................................................................73 API Access ......................................................................................................................74 Deployment ....................................................................................................................74 Conclusion ......................................................................................................................74 ■ Chapter 5: Creating Test Data with CloudKit Dashboard .....................................75 Setting Up Our Project for CloudKit ................................................................................75 Goals of Test Data ...........................................................................................................76 Creating the Parks Record Type .....................................................................................76 Creating the ParkImages Record Type ...........................................................................77 Security Role ..................................................................................................................78 Create Parks Test Data ...................................................................................................78 Create ParkImages Test Data .........................................................................................80 Conclusion ......................................................................................................................81 ■ Chapter 6: Refi ning Our Prototype .......................................................................83 Creating the Park Model .................................................................................................83 CloudKit API ....................................................................................................................84 Populating ParkListViewController .................................................................................85 vii ■ CONTENTS Setting Up Bindings ........................................................................................................86 Downloading the Thumbnail Asset .................................................................................89 Handling Selecting a Park in the List .............................................................................90 Update DetailViewController ..........................................................................................93 Downloading Park Images for the Selected Park ...........................................................94 Conclusion ....................................................................................................................100 ■ Chapter 7: Updating CloudKit Data from Our App ..............................................101 Updating Existing Data .................................................................................................101 Creating New Data .......................................................................................................104 Deleting a Park .............................................................................................................109 Deleting Park Images ...................................................................................................112 Make the Search Feature Functional ...........................................................................115 Conclusion ....................................................................................................................117 ■ Chapter 8: Adding Local Cache to Improve Performance ..................................119 Caching Park Records ..................................................................................................119 Caching and Loading Park Thumbnails ........................................................................125 Caching the Park Images .............................................................................................128 Additional Suggested Updates .....................................................................................128 Conclusion ....................................................................................................................128 Index .....................................................................................................................129 viii About the Author Bruce Wade is a software engineer from British Columbia, Canada. He started in software development when he was sixteen years old by coding his first website. He went on to study computer information systems at DeVry Institute of Technology in Calgary. To further enhance his skills, he studied visual and game programming at The Art Institute Vancouver. Over the years he has worked for large corporations as well as several startups. His software experience has led him to utilize many different technologies, including C/C++, Python, Objective-C, Swift, Postgres, and JavaScript. In 2012 he started the company Warply Designed to focus on mobile 2D/3D and OS X development. Aside from hacking out new ideas, he enjoys spending time hiking with his boxer Rasco, working out, and exploring new adventures. ix