SwiftUI Watch Connectivity — in 4 steps
https://github.com/cgaaf/SwiftUIWatchConnectivity
Step 1: Create your project
Create a new Xcode project (iOS App with Watch App) using the SwiftUI lifecycle.
Once you create the project you should have an file that looks like this:
I prefer to keep all my shared code in a single folder. This is optional
If you choose to keep code shared for iOS and WatchOS, then I recommend you delete the ContentView.swift file in both the WatchKit Extension group and the MainApp group because we will create a shared one.
Step 2: Create a shared ContentView.swift
I created a shared ContentView.swift for both the MainApp and WatchKit Extension. Don’t forget to include the extension in your targets…
Here is the code for the ContentView.swift
Step 3: Create your shared SessionDelegater
Just like before I use a single shared file for both the iOS and WatchOS targets. This will act as the required WCSessionDelegate to send messages between the watch and the phone. I use a PassthroughSubject to send data from the SessionDelegater when data is received from the other device.
Step 4: Create the counter
Now I use an ObservableObject to manage providing ContentView methods for incrementing and decrementing the counter. A message is sent to the other device whenever one increments or decrements the counter. The PassthroughSubject updates the count whenever a message is received from the SessionDelegator.
Conclusion
That’s it! Only 4 files needed to create a simple synchronized counter app using WatchConnectivity and SwiftUI