Chapters

Hide chapters

watchOS With SwiftUI by Tutorials

Second Edition · watchOS 9 · Swift 5.8 · Xcode 14.3

Section I: watchOS With SwiftUI

Section 1: 13 chapters
Show chapters Hide chapters

2. Project Structure
Written by Scott Grosch

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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 watchOS tab:

If you’re reading this book, you probably need a watch app. However, what type of app are you going to create?

  1. Does the watch app stand alone, with no corresponding iOS app?
  2. Does the watch app require a companion iOS app to function properly?
  3. 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:

  1. 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.
  2. 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.

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.

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.

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.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now