2.Project Structure¶
Written by Scott Grosch
You’ll use Xcode when creating an app to run on the Apple Watch, just like when you develop for iOS. The structure is a little different, though. In iOS, you usually have a single target for the app, but you’ll always have two when creating a watchOS app.
Start Xcode and create a new project. You’ll notice some tabs at the top of the template chooser dialog. The Multiplatform tab is the default selection.
While you could start from the App template, you shouldn’t. Instead, select the watchOStab:
If you’re reading this book, you probably need a watch app. However, what type of app are you going to create?
- Does the watch app stand alone, with no corresponding iOS app?
- Does the watch app require a companion iOS app to function properly?
- Does the watch app function on its own, but there’s also an iOS app?
Standalone App¶
Most of this book’s chapters pretend that you’ve chosen the first option. The watch app will be fully functional and won’t need a companion iOS app. As the Apple Watch is the focus of this book, I don’t want to confuse the examples with a ton of code for an iOS app that doesn’t add value. One of the later chapters will provide instructions for tying your Watch App to an iOS app if required.
Select the App template, pick a name for your project, and leave the default selection of Watch-only App. I chose Standalone for the name of the project.
Take a look at the targets, and you’ll see the template created two for you:
- Standalone is the general target for your project. It acts as a wrapper for your project so that you can submit it to the App Store.
- Standalone Watch App is the target for building the watchOS app. An app bundle contains your watchOS app’s code and assets.
You’ll seldom interact with the first target. When you think of an Apple Watch app you’re talking about the extension.
Including an iOS App¶
If you’re going to have both an iOS and a watchOS app, select the Watch App with New Companion iOS App option when creating a new project. You can choose any name you like — I chose Example — and leave the defaults for everything else. Click Next and choose a location. You’ll get Xcode’s main window.
You’ll notice Xcode still provides the same two targets as before. However, this time there are directories for both the iOS app and the watchOS app. Open the scheme editor and you will see that there are two separate schemes defined. One for building and running the iOS app, and one for building and running the watchOS app.
Not Requiring a Companion App¶
While many watchOS apps require a companion iOS project, it’s also possible for a watch app to fully function without a companion iOS app. By default, Xcode assumes your watchOS app is unable to run without an installed companion iOS app.
Note
Previous versions defaulted to running without requiring a companion app.
If your watch app can run without a corresponding iOS app, then you’ll need to make a small configuration change:
- Open the Project Navigator by pressing Command‑1, and click on the project.
- Select the watch app target.
- Select the General tab.
- In the Deployment Info section, check the Supports Running Without iOS App Installation checkbox.
Adding Apple Watch Support Later¶
It’s OK if you forget to include support for a watch app. Maybe you weren’t ready to work on the watch app yet, or perhaps you’re adding watch support to a legacy app. Maybe you just plain forgot.
With your existing iOS project open, simply add a new target via File ▸ New ▸ Target…, select the watchOS folder and then choose the App template. Make sure you select the radio button for Watch App for Existing iOS App or you may end up with a second iOS app in your project.
Note
You won’t be able to use the same product name for the watchOS target as the iOS target because Xcode would try to create conflicting names.
Having to pick a different name might be a hard stop for your team. If that’s the case, be sure to start with the watch template instead of the iOS template.
Key Points¶
- Watch apps can either stand alone without an accompanying iOS app, work independently of an iOS app, or even require an iOS app.
- You can add watch support to an iOS app at a later date.