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.
- So from the File menu select New | Project…
- 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.
- Click Next.
- Give the project the name HelloWatch, and select Swift from the Language list.
- From the Devices list, select iPhone.
- 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.
- 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.
- 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.
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:
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.
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:
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 pided 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.