Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Apple Watch Projects

You're reading from  Building Apple Watch Projects

Product type Book
Published in Feb 2016
Publisher Packt
ISBN-13 9781785887369
Pages 298 pages
Edition 1st Edition
Languages
Author (1):
Stuart Grimshaw Stuart Grimshaw
Profile icon Stuart Grimshaw

Table of Contents (17) Chapters

Building Apple Watch Projects
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Exploring the New Platform Hello Watch C-Quence – A Memory Game Expanding on C-Quence On Q – A Productivity App Watching the Weather Plot Buddy – All about Location Images, Animation, and Sound Wear It, Test It, Tweak It, Ship It This Is Only the Beginning Index

Chapter 5. On Q – A Productivity App

On Q will provide a little help to the user in remembering the bullet points of a presentation with no more than a glance at the watch, which will advance the prompts automatically without requiring interaction from the user. This app will also gather and store user data on the iPhone, and we will start to integrate the watch and phone apps to a greater extent than we have done up until now.

You will learn the following:

  • Design and implement a UI that uses multiple interface controllers

  • Use xcassets to store images

  • Use those images to provide backgrounds to UI elements

  • Add a menu that is summoned with Force Touch

  • Add feedback from the Taptic Engine to your app

  • Supply system callbacks to react to events outside the watch

Download the project template


This project starts off a little differently. In order to focus on the watchOS code, we will begin with an Xcode project that already contains the code you will need for the iPhone app. This small amount of code is pretty standard stuff, and if you have done any iOS coding with Swift before, you will have no problems understanding what the code does.

You can download the project here:

https://github.com/codingTheHole/BuildingAppleWatchProjectsBook

Broadly speaking, the phone app simply enables the user to enter any number of prompts, and perform the standard re-order and delete tasks, which would be a troublesome task (to say the least) on the watch itself.

When you have downloaded and opened the project, you will find the complete iOS code, but for the WatchKit Extension and WatchKit app there is no more than the standard template files that you know already from our previous projects. By all means, have a look over the iPhone app source code. Anything that does...

Plan the Watch app


Before we code, we plan. An hour's planning will generally save many hours' wasted coding. This is a good thing, and we've mentioned it before. We might even mention it again.

Mission statement

We will create an app that will prompt the user with small snippets of text for use in say, a presentation or lecture, or any other scenario in which she might otherwise rely on cue cards or similar. Each prompt screen can be swiped to a second screen that shows more detailed, scrollable text. The prompts can be color-coded if desired, perhaps as an indication to progress a slide presentation, or hand out printed material and so on..

The prompts can be advanced manually by tapping forward and backward buttons, or automatically each time the user raises her wrist to glance at the watch screen. The succession of prompts can be stopped by using Force Touch to show a menu, from which the user can stop or continue the sequence.

The text of the prompts (and the detailed text that accompanies...

Setting up the project


In this chapter, we are once again dealing with two apps in parallel, one for watchOS and one for iOS (which has been coded for you, but we will refer to it frequently). All but one of the files (SharedConstants.swift) will be used by one target (that is, one of the apps) or the other, and we must ensure that these targets are correctly selected for each file that we create within the project.

Create the required watchOS classes

We will begin by creating the Swift files that we will need. Once this is done, you will have a good overview of the overall structure of the project and an idea of how the pre-coded iOS files relate to the watchOS app you will be writing.

In the project navigator, select the On Q WatchKit Extension group so that the files we create below will automatically select the WatchKit target in the file creation dialog (where we selected both the On Q and On Q WatchKit Extension targets above) and then perform the following steps:

  1. Create a new Swift file...

Writing the code


So, time to get some code written. We will start with some code that should look very familiar.

WatchConnectivity

Once again, we need to create a class that encapsulates all of the logic around communication with the outside world (or at least the part of the outside world that is squeezed into an iPhone).

WatchConnectivityManager class

In the project navigator, select the WatchConnectivity.swift file. Delete the import Foundation line from the code, and replace it with the following code:

import WatchConnectivity

class WatchConnectivityManager: NSObject, WCSessionDelegate {

    static let sharedManager = WatchConnectivityManager()

    let dataManager = WatchDataManager.sharedManager

    private override init() {
    }
    
    func session(session: WCSession,didReceiveApplicationContext applicationContext: [String : AnyObject]) {
    }
}

Don't be alarmed by the error warning that Xcode shows about the dataManager; this is only happening because we haven't yet written the...

Testing with the iPhone and real data


We are now ready to test entering user data into the iOS app (which should work without issues—you downloaded it from the website, after all) and to test the transfer of that data to the watchOS app.

If you have already run the iPhone app and tested what the buttons do (we're developers, we're naturally curious, OF COURSE you've played around with it), then you will already have sent some default data to the Watch. Let's get rid of that first by deleting the app from the iOS Simulator.

Now we're all starting from the same point.

Unstub the WatchDataManagerinit method

Although we have stored prompts data to NSUserDefaults in the updatePrompts method, we still have a dataManager that returns stubbed data, so let's fix that now. Replace the init of the current WatchDataManager implementation with the following code:

private init(){
    if let storedPrompts = NSUserDefaults.standardUserDefaults().objectForKey(kPromptsKey) as! [Prompt]? {
            self.prompts...

Challenges for further growth


Time to jam around with the code, since an essential part of becoming familiar with any platform is building up a wealth of experience of what is possible, what is worth repeating and what doesn't work at all.

  • Try changing the appearance of the app in more fundamental ways than simple changes of color (critical though that can be). Is there something you can do to make those labels more engaging? Can you fit the app's appearance to some (possibly invented) corporate image? Could the layout be improved?

  • Can we add a button to the launch screen that will disable the auto-advance feature? We'd need to store that preference too.

  • How would the user experience improve if we added a little animation magic to the transition from the launch screen's starting UI and its prompting UI? Any other candidates for animation?

  • If you already have more than rudimentary experience with developing for the iPhone, you might wish to add the ability to store and recall multiple Prompt...

Summary


In this chapter, you have learned to add navigation to separate screen pages using IB Segues, and you have created separate subclasses of WKInterfaceController to manage those pages. You have used Xcode's xcassets to add images to your project and learned how to use those images in your app's UI, and you have used the Apple Watch's Taptic Engine to add Haptic feedback, thus adding another layer of communication with the user.

In Chapter 6, Watching the Weather we will be looking at using NSURLSession to get the Watch linked to the internet, using other data transfer technologies when transferring data back to the phone from the watch, and we will also add a whole new source of interaction with your app, the so-called Glance screen.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Building Apple Watch Projects
Published in: Feb 2016 Publisher: Packt ISBN-13: 9781785887369
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}