Skip to content

Instantly share code, notes, and snippets.

@danallenio
Created June 24, 2023 01:12
Show Gist options
  • Select an option

  • Save danallenio/ff5926bc7d9817881cbc3bfd9ed806ac to your computer and use it in GitHub Desktop.

Select an option

Save danallenio/ff5926bc7d9817881cbc3bfd9ed806ac to your computer and use it in GitHub Desktop.
How to add a WatchOS app to a Multiplatform project and share the SwiftUI app lifecycle and root views

WatchOS Multiplatform Setup

How to add a WatchOS app to a Multiplatform project and share the SwiftUI app lifecycle and root views.

The WatchOS target

  1. File > New > Target…
  2. Under the WatchOS tab, select App and press Next.
  3. Set the product name. Right now Xcode requires a different name than the existing multiplatform target but you can change it to match in a later step. You'll probably want to set your watch app to be "Watch App for Existing iOS App" for this workflow. Click Finish.
  4. Select your .xcodeproj and you'll see a new Watch App target (and test targets if you elected for them) in the Targets section of the sidebar.
  5. Select the Watch App and on the General tab you can change the Display Name to match your other targets.
  6. Click the Watch App target again while it's highlighted to edit the name. I changed mine from "{{Name}}Watch Watch App" to just "{{Name}} Watch App".

Allow watch app to run independently of the iOS app

Under the {{Name}} Watch App target General tab, Deployment Info section, check "Supports Running Without iOS App Installation".

Have the watch app use the same shared SwiftUI app lifecycle

Delete the {{Name}}App.swift and ContentView.swift files in the Watch App directory. Then add the watch target to the lifecycle file and any views using the inspector.

If you included Core Data with the template then you'll also need to add the watch target to the persistence logic and model definition files.

Finishing touches

I've had a few directory renames cause errors but Xcode was able to point me to where I needed to update configurations. Don't hesitate to share your solutions so we can all learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment