39,59 €
Build real-world applications for the Apple Watch platform using the WatchKit framework and Swift 2.0
This book is for developers who are interested in creating amazing apps for the Apple Watch platform. Readers are expected to have no prior experience of programming.
Wearable are the next wave of mobile technology and with the release of Apple's WatchKit SDK, a whole new world of exciting development possibilities has opened up.
Apple Watch App Development introduces you to the architecture and possibilities of the Apple Watch platform, as well as an in-depth look at how to work with Xcode playgrounds. Benefit from a rapid introduction to the Swift programming language so you can quickly begin developing apps with the WatchKit framework and the Xcode Development IDE. Get to grips with advanced topics such as notifications, glances, iCloud, Apple pay, closures, tuples, protocols, delegates, concurrency, and using Swift Playgrounds, with each concept is backed up with example code that demonstrates how to properly execute it. Finally, discover how to package and deploy your Watch application to the Apple AppStore.
By the end of this book, you will have a good understanding of how to develop apps for the Apple Watch platform, and synchronize data using iCloud between the wearable and the iOS device.
This book takes a step-by-step approach to developing applications for the Apple Watch using the Swift programming language and the WatchKit UI. Each topic is explained in a conversational and easy-to-follow style.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 321
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2016
Production reference: 1250416
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-636-2
www.packtpub.com
Author
Steven F. Daniel
Reviewer
Dylan Marriott
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Reshma Raman
Content Development Editor
Priyanka Mehta
Technical Editor
Taabish Khan
Copy Editor
Shruti Iyer
Project Coordinator
Izzat Contractor
Proofreader
Safis Editing
Indexer
Rekha Nair
Graphics
Abhinash Sahu
Production Coordinator
Melwyn Dsa
Cover Work
Melwyn Dsa
Steven F. Daniel is the CEO and founder of GENIESOFT STUDIOS, a software development company that is based in Melbourne, Victoria, which focuses primarily on developing games and business applications for iOS, Android, and Mac OS X. Steven is a software developer with more than 14 years of experience developing desktop and web-based applications for a number of companies, including ANZ, Department of Justice, BP Australia, and AXA Australia.
Steven is always interested in emerging technologies, and he is a member of the SQL Server Special Interest Group (SQLSIG), Melbourne CocoaHeads, and the Java Community. He was the co-founder and Chief Technology Officer (CTO) of SoftMpire Pvt Ltd., a company that focused primarily on developing business applications for the iOS and Android platforms.
Steven is the author of various book titles: Xcode 4 iOS Development Beginner's Guide, iOS 5 Essentials, iPad Enterprise Application Development Blueprints, Xcode 4 Cookbook, and Android Wearable Programming.
Check out his blog at http://www.geniesoftstudios.com/blog/, or follow him on Twitter at http://twitter.com/GenieSoftStudio.
No book is the product of just the author; he just happens to be the one with his name on the cover. A number of people have contributed to the success of this book, and it would take more space than I have to thank each one individually.
I would personally like to thank two special people who have been an inspiration and who have provided me with so much support during the writing of this book. The first is Reshma Raman, my Senior Acquisition Editor, who is the reason that this book exists, and the second is Priyanka Mehta for her understanding and support as well as her brilliant suggestive approaches during the chapter rewrites. I would like to thank both of you for everything and making the writing process enjoyable.
To my reviewers, thank you so much for your valued suggestions and improvements to make this book what it is, I am truly grateful to each and every one of you.
Thank you to the entire Packt Publishing team for working so diligently to help bring out a high quality product. A big shout out to the engineers at Apple for creating the Apple Watch and the WatchKit Platform to provide developers with the tools to create fun and sophisticated applications.
Finally, I would like to thank all of my friends for their support, understanding, and encouragement during the book writing process. I am extremely grateful to have you as my friends, and it is a privilege to know each and every one of you.
Dylan Marriott is a young iOS developer from Switzerland. At the start of his career, he worked with web technologies, but he soon got involved with mobile and never went back. He started off with Android and then transitioned toward iOS. He currently works at Evernote, where he gets to work with the newest iOS technologies. In his free time, he has published a number of iOS and Apple Watch apps.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
This book is dedicated to:
My favorite uncle Benjamin Jacob Daniel, thank you for always making me smile and for inspiring me to work hard and achieve my dreams. You are a true inspiration, and I couldn't have done this without your love, support, and guidance. Thank you.
Chan Ban Guan, for the continued patience, encouragement, and support, and most of all, for believing in me during the writing of this book.
I would like to thank my family for their continued love and support and for always believing in me throughout the writing of this book. This book would not have been possible without your love and understanding, and I would like to thank you from the bottom of my heart.
With the increasing amount of new wearable devices hitting the market, wearables are the next wave of mobile computing technology. After the release of Apple's WatchKit SDK, a whole new world of exciting development possibilities awaits us.
Apple Watch App Development provides you with a practical approach that shows you how to develop and build Apple Watch apps using the Xcode Integrated Development Environment. You will be introduced to the architecture and limitations of the Apple Watch platform, followed by an in-depth look at the Swift programming language, where we talk about closures, tuples, protocols, delegates, and how to work with Xcode playgrounds.
We also discuss more advanced topics, such as notifications, glances, Apple Pay, as well as the Core Location and Watch Connectivity frameworks to synchronize data between the Apple Watch and the iPhone using the WatchKit platform. By the end of this book, you will have a good understanding of how to build real-world applications for the Apple Watch platform using the WatchKit framework and Swift 2.0.
In this book, I have tried my best to keep the code simple and easy to understand by providing a step-by-step approach with lots of screenshots at each step to make it easier to follow. You will soon be mastering the different aspects of Apple Watch wearable programming, as well as mastering the technology and skills that are needed to create your own applications for the Apple Watch platform.
Feel free to contact me at <[email protected]> for any queries, or just drop me an e-mail to say "Hello".
Chapter 1, Introducing the Swift Programming Language, focuses on how to go about signing up to the Apple Developer program, as well as downloading and installing the Xcode development tools. We will cover Xcode playgrounds before moving on to learning the basics of the Swift programming language and some of the new additions to Swift with Swift 2.
Chapter 2, Understanding Apple Watch, introduces you to the WatchKit platform architecture and the application life cycle, as well as a list of the WatchKit classes that make up the WatchKit platform. You'll learn about some of the limitations behind the WatchKit platform architecture, as well as some of the new additional features that are a part of watchOS 2. You will then go on to learn about the Apple Watch Human Interface Guidelines.
Chapter 3, Exploring WatchKit Controls, introduces you to some of the controls that come as a part of the WatchKit platform. You will learn how to work with Xcode's Interface Builder to build the visual interface for our first WatchKit application. You will also learn how to use Outlets to bind objects to the Interface Builder objects and create some Action events to respond to these objects when tapped by the user to provide visual feedback.
Chapter 4, Using the Core Location and Watch Connectivity Frameworks, delves deeper into learning more about the WatchKit framework and the new layout system using groups. You'll learn about the MapKit and WKInterfaceMap frameworks. You will also learn how you can use these to incorporate mapping capabilities within your WatchKit apps using the Watch Connectivity framework to send geographical location-based coordinates between your iOS app and WatchKit to display the user's current location on the Apple Watch.
Chapter 5, Navigating Around in WatchKit, introduces you to some of the ways that you can navigate within the watch face user interface. You'll learn the differences between page-based, modal, and hierarchical navigation, and when to use them. You will then learn how to use the HealthKit framework to access health-related information and share this information between the iOS device and the WatchKit extension to display a user's current heart rate, blood type, and number of steps taken for a particular day.
Chapter 6, Implementing Tables within Your App, focuses on teaching you how to create applications that handle Apple Pay payments using the PassKit framework. You will learn how to pass information between the iOS device and the WatchKit extension. You will also learn how to use the WKInterfaceTable controller, how to set it up and configure it, as well as how to display information from a JSON file.
You will learn how to use the WKInterfaceTable methods to determine when a row has been selected by the user and respond to this action.
Chapter 7, Adding Menus to Your App, introduces you to WatchKit menus, and how you can incorporate them into your own applications, as well as respond to them based on what the user has chosen. We end the chapter by covering how you can use the Taptic Engine to integrate haptic feedback in your own applications.
Chapter 8, Incorporating the Glance Interface in Your App, shows you how to effectively incorporate WatchKit glances within an existing WatchKit application by creating a custom GlanceInterfaceController class to display information within the glance interface. You'll learn about the glance interface guidelines, which outline the guidelines and principles that you need to follow when designing your layouts for the different Apple Watch screen sizes.
Chapter 9, Incorporating Notifications within Your App, explains how to incorporate and configure local and remote notifications within an existing Apple Watch application. You will learn about the notifications life cycle and the differences between static and dynamic interfaces.
You will learn about the Apple Push Notification service (APNs) and how you can use this to send and receive information by responding to button actions when a user taps on the button. You will learn about the notification interface guidelines, which outlines the guidelines and principles that you need to follow when designing your notifications.
Chapter 10, Image Compression and Animation, shows you how to add images to the image assets catalog, and then start to build a simple WatchKit application that showcases how to animate a series of images within the WatchKit user interface.
You'll learn about the Application Transport Security (ATS) protocol that Apple introduced with the release of iOS 9, which forces developers to provide secure connections between your Apple Watch apps that communicate over HTTPS. We will also be covering how to properly configure our Apple WatchKit extension, as well as learn the best ways of handling and compressing large image files when these are downloaded from an external website so to increase performance.
Chapter 11, Packaging and Deploying Your App, focuses on how to submit your application to the Apple App Store, and share your creations with the rest of the community. You'll learn how to set up your iOS development team as well as the development and distribution certificates.
Finally, you will learn how to use the Instruments application to profile your application to eliminate performance bottlenecks. You will also learn how to package your application using Xcode and deploy this to iTunes Connect so that you can download and test on your Apple Watch device.
The minimum requirement for this book is an Intel-based Macintosh computer running OS X El Capitan 10.11. We will be using Xcode 7.3, which is the Integrated Development Environment (IDE) that is used to create applications for iOS, watchOS, and tvOS. Almost all projects that you will create with the help of this book will work and run on the iOS Simulator. However, some projects will require an Apple Watch and an iPhone to work correctly.
You can download the latest version of Xcode from http://developer.apple.com/xcode/.
This book is intended for developers who have a working experience of application development principles on the Mac OS X and/or iOS platforms and wish to expand their knowledge to develop applications for the Apple Watch using the Swift programming language and the WatchKit framework. It is assumed that you are familiar with object-oriented programming (OOP) and have some experience with Swift.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
At WWDC 2014, Apple introduced a brand new programming language called Swift. The Swift programming language brings concise syntax, type safety, and modern programming language features to Mac and iOS developers.
Since its release, the Apple developer community has responded with great excitement, and developers are rapidly starting to adopt this new language within their own applications. The Swift language is the future of developing on Apple's platforms.
This chapter includes the following topics:
Before you can begin building iOS applications for your iOS devices, you must first join as a registered user of Apple Developer Program in order to download all of the necessary components to your computer. The registration process is free and provides you with access to the iOS SDK and other developer resources that are really useful to get you started.
The following short list outlines some of the things that you will be able to access once you become a registered member of Apple Developer Program:
Whether you develop applications for the iPhone or iPad, these use the same OS and iOS SDK that allows you to create universal apps that will work with each of these devices. On the other hand, Apple Watch uses an entirely different OS called watchOS.
To prepare your computer for iOS development, you need to register as an Apple developer. This free process gives you access to the basic levels of development that allow you to test your app using iOS Simulator without the ability to sell your app on the Apple App Store. The steps are as follows:
You will then become an official member of Apple Developer Program. You will then be able to download beta software so that you can test them on your actual device hardware as well as having the freedom to distribute your apps to your end users.
In the next section, we will look at how to download and install Xcode development tools.
In this section, we will take a look at what Integrated Development Environments (IDEs) and Software Development Kits (SDKs) are needed to develop applications for the iOS platform, which is Apple's operating system for mobile devices. We will explain the importance of each tool's role in the development cycle and the tools required to develop applications for the iOS platform, which are as follows:
Component
Description
Xcode
This is the main IDE that enables you to develop, edit, and debug your native applications for the iOS and Mac platforms using the Objective-C or Swift programming languages.
iOS Simulator
This is a Cocoa-based application that enables you to debug your iOS applications on your computer without the need of having an iOS device. There are many iOS features that simply won't work within Simulator, so a device is required if an application uses features such as the Core Location and MapKit frameworks.
Instruments
These are the analysis tools that help you optimize your applications and monitor memory leaks during the execution of your application in real time.
Dashcode
This enables you to develop web-based iOS applications and dashboard widgets.
Once you are registered, you will need to download and install Xcode developer tools by performing the following steps:
You can find additional development tools from the Apple developer website at https://developer.apple.com/.
In the next section, we will be looking at what, exactly, Xcode playgrounds are and how you can use them to experiment with designing code algorithms prior to incorporating the code into your project. So, let's get started.
A playground is basically an interactive Swift coding environment that displays the results of each statement as updates are made without having the need to compile and run a project. You can use playgrounds to learn and explore Swift, prototype parts of your app, and create learning environments for others. The interactive Swift code environment lets you experiment with algorithms, explore system APIs, and even create your very own custom views without the need to create a project. Once you perfect your code in the playground, simply move this code into your project. Given that playgrounds are highly interactive, they are a wonderful vehicle for distributing code samples with instructive documentation and can even be used as an alternative medium for presentations.
With the new Xcode 7 IDE, you can incorporate rich text comments with bold, italic, and bulleted lists with the addition of having the ability to embed images and links. You can even embed resources and support Swift source code in the playground to make the experience incredibly powerful and engaging, while the visible code remains simple.
Playgrounds provide you with the ability to do the following:
Let's begin by opening the Xcode IDE and explore how to create a new playground file for the first time. Perform the following steps:
Once your project is created, you will be presented with the default playground template, as shown in the following screenshot:
In the next section, you will begin learning about some of the Swift language basics, start adding lines of code within this playground file, and see the results that we get when they are executed.