Learning Watchkit Programming Addison-Wesley Learning Series
Total Page:16
File Type:pdf, Size:1020Kb
Learning WatchKit Programming Addison-Wesley Learning Series Visit informit.com/learningseries for a complete list of available publications. The Addison-Wesley Learning Series is a collection of hands-on program- ming guides that help you quickly learn a new technology or language so you can apply what you’ve learned right away. Each title comes with sample code for the application or applications built in the text. This code is fully annotated and can be reused in your own projects with no strings attached. Many chapters end with a series of exercises to encourage you to reexamine what you have just learned, and to tweak or adjust the code as a way of learning. Titles in this series take a simple approach: they get you going right away and leave you with the ability to walk off and build your own application and apply the language or technology to whatever you are working on. Learning WatchKit Programming A Hands-On Guide to Creating Apple Watch Applications Wei-Meng Lee New York • Boston • Indianapolis • San Francisco Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are Editor-in-Chief claimed as trademarks. Where those designations appear in this book, and the publisher was Mark L. Taub aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. Senior Acquisitions Editor The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. Trina MacDonald No liability is assumed for incidental or consequential damages in connection with or arising out Development Editor of the use of the information or programs contained herein. Sheri Cain For information about buying this title in bulk quantities, or for special sales opportunities (which Managing Editor may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales John Fuller department at [email protected] or (800) 382-3419. Full-Service For government sales inquiries, please contact [email protected]. Production Manager Julie B. Nahil For questions about sales outside the United States, please contact [email protected]. Copy Editor Visit us on the Web: informit.com/aw Stephanie Geels Library of Congress Control Number: 2015940909 Indexer Copyright © 2015 Pearson Education, Inc. Jack Lewis All rights reserved. Printed in the United States of America. This publication is protected by copy- Proofreader right, and permission must be obtained from the publisher prior to any prohibited reproduction, Anna Popick storage in a retrieval system, or transmission in any form or by any means, electronic, mechan- ical, photocopying, recording, or likewise. To obtain permission to use material from this work, Technical Reviewers please submit a written request to Pearson Education, Inc., Permissions Department, 200 Old Mark H. Granoff Tappan Road, Old Tappan, New Jersey 07675, or you may fax your request to (201) 236-3290. Chaim Krause Apple, the Apple logo, Apple TV, Apple Watch, Cocoa, Cocoa Touch, eMac, FaceTime, Finder, Niklas Saers iBook, iBooks, iCal, Instruments, iPad, iPad Air, iPad mini, iPhone, iPhoto, iTunes, the iTunes logo, iWork, Keychain, Launchpad, Lightning, LocalTalk, Mac, the Mac logo, MacApp, MacBook, Editorial Assistant MacBook Air, MacBook Pro, MacDNS, Macintosh, Mac OS, Mac Pro, MacTCP, the Made for Olivia Basegio iPad logo, the Made for iPhone logo, the Made for iPod logo, Metal, the Metal logo, the Monaco Cover Designer computer font, MultiTouch, the New York computer font, Objective-C, OpenCL, OS X, Passbook, Pixlet, PowerBook, Power Mac, Quartz, QuickDraw, QuickTime, the QuickTime logo, Retina, Chuti Prasertsith Safari, the Sand computer font, Shake, Siri, the Skia computer font, Swift, the Swift Logo, the Compositor Textile computer font, Touch ID, TrueType, WebObjects, WebScript, and Xcode are trademarks of Apple, Inc., registered in the United States and other countries. OpenGL and the logo are CIP Group registered trademarks of Silicon Graphics, Inc. Intel, Intel Core, and Xeon are trademarks of Intel Corp. in the United States and other countries. ISBN-13: 978-0-13-419544-5 ISBN-10: 0-13-419544-2 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, June 2015 ❖ I dedicate this book with love to my family, and to my dearest wife, who has had to endure my irregular work schedule and take care of things while I was trying to meet writing deadlines! ❖ This page intentionally left blank Contents at a Glance Preface xiii Acknowledgments xvii About the Author xix 1 Getting Started with WatchKit Programming 1 2 Apple Watch Interface Navigation 17 3 Apple Watch User Interface 45 4 Interfacing with iOS Apps 99 5 Displaying Notifications 149 6 Displaying Glances 179 Index 195 This page intentionally left blank Contents Preface xiii Acknowledgments xvii About the Author xix 1 Getting Started with WatchKit Programming 1 Specifications of the Apple Watch 1 Getting the Tools for Development 2 Understanding the WatchKit App Architecture 3 Deploying Apple Watch Apps 4 Interaction between the Apple Watch and iPhone 4 Communicating with the Containing iOS App 5 Types of Apple Watch Applications 6 Hello, World! 6 Creating an iPhone Project 6 Adding a WatchKit App Target 8 Examining the Storyboard 11 WatchKit App Lifecycle 12 Modifying the Interface Controller 13 Running the Application on the Simulator 14 Summary 16 2 Apple Watch Interface Navigation 17 Interface Controllers and Storyboard 17 Lifecycle of an Interface Controller 19 Navigating between Interface Controllers 22 Hierarchical Navigation 23 Page-Based Navigation 27 Passing Data between Interface Controllers 28 Customizing the Title of the Chevron or Cancel Button 34 Navigating Using Code 35 Presenting a Series of Pages 38 Changing the Current Page to Display 40 Summary 43 xContents 3 Apple Watch User Interface 45 Responding to User Interactions 45 Button 46 Switch 59 Slider 62 Displaying Information 65 Labels 65 Images 65 Table 71 Gathering Information 82 Getting Text Inputs 82 Getting Emojis 85 Laying Out the Controls 86 Force Touch 91 Displaying a Context Menu 91 Adding Menu Items Programmatically 97 Summary 98 4 Interfacing with iOS Apps 99 Localization 99 Localizing the User Interface 102 Creating Localizable Strings 106 Using the Date Control 112 Communicating between the WatchKit App and the Extension 113 Location Data 114 Displaying Maps 123 Accessing Web Services 126 Sharing Data 130 Summary 148 5 Displaying Notifications 149 What Is a Notification? 149 Types of Notifications on the Apple Watch 152 Implementing the Short-Look Interface 153 Implementing the Long-Look Interface 167 Summary 178 Contents xi 6 Displaying Glances 179 What Is a Glance? 179 Implementing Glances 180 Customizing the Glance 182 Testing the Glance 186 Making the App Useful 186 Creating a Shared App Group 187 Implementing Background Fetch 188 Updating the Glance 192 Summary 194 Index 195 This page intentionally left blank Preface Welcome to Learning WatchKit Programming! This is an exciting time to be a programmer, as we are witnessing a new era of wear- ables. While the Apple Watch is not the first wearable device in the market, its launch signified the intention of Apple to enter the wearable market in a big way. After success- fully changing various industries—music, computer, phone, and mobile computing— Apple looks set to change the wearable industry. And nobody is taking this lightly. As with the iPhone, much of the usefulness and functionality of the Apple Watch device actually come from the creativity of the third-party developers. In the early days of the iPhone, Apple restricted all third-party apps to web applications, as they wanted to retain the monopoly on developing natively for the device. However, due to the over- whelming protests of developers, Apple finally relented by releasing an SDK to support third-party apps. It was this decision that changed the fate of the iPhone; the iPhone would never have been so successful without the ability to support third-party apps. When the Apple Watch was announced, Apple was quick to learn its lesson and real- ized that the success of the Apple Watch largely depends on the availability of apps that support it. Hence, before the release of the Apple Watch, the SDK was made available to developers to have a hand in developing Apple Watch apps. The book you are holding in your hands right now (or reading on your phone or tablet) is a collection of tutorials that help you navigate the jungle of Apple Watch programming. This book contains all of the fundamental topics that you need to get started in Apple Watch programming. As this is a book on Apple Watch programming, I am going to make a couple of assumptions about you, the reader: n You should already be familiar with the basics of developing an iOS application. In particular, concepts like outlets and actions should not be new to you. n You should be comfortable with the Swift programming language. See the next section on how to get started with Swift if you are new to it. What You’ll Need To get the most out of this book: n You need a Mac, together with Xcode. n Your Mac should be running at least Mac OS X Yosemite (v10.10), or later. xiii xiv Preface n You can download the latest version of Xcode from the Mac App Store. All the code samples for this book are tested against Xcode 6.3. n If you plan to test your apps on a real device, you need to register to become a paid iOS developer (https://developer.apple.com/programs/ios/).