Chapter 2. Hello Watch

Time to fire up the engine and get a feel for some of the new tools we have at our disposal, courtesy of Apple's Xcode development environment. Once we get started, you will recognize much of what you already know through using Xcode and Swift to build apps for iOS, whether for iPhone, iPad, or universal apps for both. The project structure will be familiar to you, and everything that you would expect to see in a purely iOS project will be present.

If you have so far been developing for OS X and not iOS, you will notice many similarities but also many differences to an OS X project. Not a problem. You know Swift already, we will be setting up the projects step by step, and we are all new to WatchKit.

Briefly, we will learn how to do the following:

  • Start and set up a project using an Xcode template
  • Create some UI elements
  • Get the app running on the Watch Simulator
  • Add some simple animation to the UI
  • Get basic input from the user

By the end of this chapter you will be looking at a simple but functioning WatchKit app, installed and running on the Watch simulator.

Setting it all up

This will be largely familiar territory to anyone who has already created projects for iOS, with just a couple of things to bear in mind, which will be pointed out as we go along. Do note, however, that if you have not yet updated to Xcode 7 and there are many professional environments in which updating isn't done without a significant degree of caution—then now is the time to do it, Xcode 6 will not build WatchKit apps beyond the first version of watchOS and we will be developing for the vastly superior watchOS 2.

Creating a new Xcode project

Assuming you have Xcode 7 up and running, we will now let it do much of the work involved in setting up a project that will include the targets, schemes and source files needed to create the basics of a Watch app and its companion iPhone app.

iPhone, by the way, not iPad. Only the iPhone (5 and above) can pair with the Watch, and if the app is run on an iPad, the presence of the Watch app will simply be ignored.

A template is provided by Xcode that will do all this, and though we will look later at other ways of adding a WatchKit target, we will stick with the easiest set up at first.

  1. So from the File menu select New | Project…
    Creating a new Xcode project
  2. Select the Application template from the watchOS section.

    Note that we now have the new entry, watchOS, in the list of available platforms; be sure not to choose Apple Watch from the top section, iOS, which is for creating apps for the original watchOS version.

  3. Click Next.
    Creating a new Xcode project
  4. Give the project the name HelloWatch, and select Swift from the Language list.
  5. From the Devices list, select iPhone.
  6. Deselect all other options as shown.

    This is to create as simple and uncluttered a project as possible, and is not intended to discourage the use of unit and UI tests. Similarly, we will be adding other scenes (views) in later projects, but we don't need them at this stage.

  7. Click Next.

    Now save the project file to wherever is appropriate for you. If you usually use version control, there is no reason to do otherwise here, and if you don't, don't worry about it, you can leave it unselected. And there is no need to add the project to another workspace.

  8. Hit Create and you're done! You now have a fully functioning—though not very functional—Apple Watch app project set up and ready to be turned into something a little more, well, interesting.
    Creating a new Xcode project

Check out what's new

But before we do that, let's have a look around the project and see what we have set up thus far. Your project window should look very similar to the image here:

Check out what's new

Building and running the app

If you hit the run button (Command-R) now, you'll probably be pleased to note that the app compiles, builds, and runs without complaint. You'll probably also be a little underwhelmed at the result, namely, a blank app window will open in iOS Simulator, which you will have seen before many times no doubt. And that's it. So where's the watch?

If you look carefully at the screenshot below, you will see that the selected Scheme is running the iPhone app target. One of the many things that Xcode saved you having to do yourself was creating a scheme for the WatchKit app target, but it is not yet selected.

Building and running the app

Select the WatchKit app scheme, and choose one of the phone/watch combinations. I tend to use the smaller watch, the logic being if it's readable on the small one, the larger one will generally present no extra challenges. But whatever suits is fine.

When you run this scheme, one of two things may happen:

If the watchOS Simulator launches, and after showing a watch face for a second or so presents a black screen, with the time in the upper right hand corner, you're in luck. The WatchKit app has been installed and is running, as shown below:

Building and running the app

If the clock face is a different one, don't worry, it doesn't matter which is showing. You can even change it, if you like.

However, if you are taken immediately back to the watch face, don't panic. At the time of writing, the watchOS Simulator sometimes needs a second run at it. Just hit Run again, and all will be as it should be, and you'll be looking at the image above right.

From now on, running from this scheme will launch the iOS Simulator (and, if necessary, install the iPhone app) as well as the Watch Simulator.

Note

Two Simulators?

You may have noticed that you now have two Simulator apps running (check out the dock). Now that iOS Simulator has to deal with both iOS and watchOS, you will see that it has been divided into just plain 'Simulator' and 'Simulator (Watch)', each part running as a separate app with a separate icon in the dock.

However, to preserve clarity, we will refer to the iOS Simulator and Watch Simulator (Apple have unfortunately decided that such clarity is unnecessary when it comes to the dock icons of the two apps).

iOS Simulator will not come to the foreground, so to see it you'll need to select it from the dock. You may be surprised to see that the phone app is not running; you are simply looking at the home screen. This is because although iOS Simulator is necessary to run the watch app in watchOS Simulator, it is not necessarily the case that the companion phone app needs to be running.

We will look at this again later, for the moment we don't need to deal directly with iOS Simulator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.82.21