Chapters

Hide chapters

SwiftUI Cookbook

Live Edition · iOS 16.4 · Swift 5.8.1 · Xcode 14.3.1

Create a watchOS App with SwiftUI
Written by Team Kodeco

Building a watchOS app is an exciting prospect, and SwiftUI simplifies this task to an impressive extent. With SwiftUI, you can craft compelling and smooth user interfaces that function seamlessly on all Apple platforms.

Begin by adding a watchOS target to your project in Xcode. Select FileNewTarget, then select the watchOS tab and choose App from the Application section of the template picker, then click Next. Name your project, then activate the new scheme when prompted.

Note: To follow along with this entry, it’s fine to leave the default selection of creating a Watch-only app. In practice, you may want to set up a Watch app and a new companion iOS app in an existing project or just add a companion Watch app to an existing iOS app project, for which you would select the appropriate option for your new target.

After your project setup is complete, you can immerse yourself in designing your app’s user interface with SwiftUI. Inside the ContentView.swift file that’s in your Watch App folder, you’ll find a default view pre-built for you. Feel free to modify this view or construct your own to fit your app’s requirements.

Below is an example showcasing the creation of a simple watchOS app, which shows a welcoming message upon launch:

struct ContentView: View {
  var body: some View {
    Text("Welcome to my watchOS app!")
  }
}

Here’s what your preview should look like:

A Watch app in SwiftUI.
A Watch app in SwiftUI.

In this code snippet, you design a ContentView that displays a Text view with a warm greeting.

After writing your code, you can run your watchOS app in the simulator to get a feel of its interface on an actual device. To do so, simply choose your watchOS app target from the Xcode scheme menu and click the Run button.

Congratulations on crafting your first watchOS app with SwiftUI! Now, you can delve deeper into SwiftUI’s potential for building immersive and intuitive user interfaces across all Apple platforms.

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.